Play Audio
TheAG SDK is a dedicated component for rendering effects. However, please note that the Community Edition does not support audio and video-related content. If you require audio play functionality, we recommend utilizing the PAG Enterprise Edition.
The PAG Enterprise Edition (with a movie suffix in the package name) incorporates the Movie function, enabling users to effortlessly read and play audio. It supports playing the built-in audio in the PAG file, as well as the audio in videos filled with placeholder images. Users have the flexibility to directly use PAGView for play or process PCM audio data on their own.
If you export a PAG file containing audio, you can refer to Export Audio
Audio Player Component PAGView
The Enterprise Edition PAGView has integrated the audio play capability. It supports the play of audio from PAG files as well as video files that contain placeholder images.
The detailed documentation of PAGImage can be found in Android | iOS .
Get Audio Data
The PAGAudioReader class in the Enterprise Edition offers an interface for accessing audio frame data in PAGComposition. It allows you to obtain mixed audio data, which includes audio from PAG files as well as video files that contain placeholder images. The audio data format used is PCMSigned16.
Usage Examples
1、Build a PAGAudioReader instance
Code Examples
android:
// Get the reader instance of the specified audio parameter
int sampleRate = 48000;
int sampleCount = 1024;
int channels = 2;
PAGAudioReader reader = PAGAudioReader.Make(sampleRate, sampleCount, channels);
iOS:
// Get the reader instance of the specified audio parameter
NSInteger sampleRate = 48000;
NSInteger sampleCount = 1024;
NSInteger channels = 2;
CGFloat volume = 1.0f; // 0 ~ 1.0f
PAGAudioReader *reader = [PAGAudioReader MakeWithSampleRate:sampleRate sampleCount:sampleCount channels:channels volume:1.0f];
2、 Set PAGComposition instance
Code Examples
Android:
PAGAudioReader reader = PAGAudioReader.Make(sampleRate, sampleCount, channels);
PAGComposition composition = PAGFile.Load(getAssets(), selectedPAGFileName);
reader.setComposition(composition);
iOS:
PAGComposition *composition = [PAGFile Load:path];
[reader setComposition:composition];
3、seek(optional)
Android:
// Seek to the position of the 5th second
long positionUs = 5_000_000;
reader.seek(positionUs);
iOS:
NSInteger positionUs = 5*1000*1000;
[reader seek:positionUs];
4、Read audio
You can read one frame of audio through the readNextSample interface of PAGAudioReader and move it to the next frame simultaneously;
To read the complete audio data from a PAGComposition instance, you can simply loop through the readNextSample call until all audio data is read.
Because composition allows for real-time modification, when there is no audio data in the composition, the audioReader will still return audio frames but the data in the audio is 0.
Code Examples:
Android:
if (reader.isEmpty()) {
// If reader.isEmpty() returns true, it indicates that the composition has no audio data.
return;
}
// Read the audio data in a loop until the reading is complete
PAGAudioSample audioFrame;
while ((audioFrame = reader.readNextSample()) != null
&& audioFrame.timestamp + audioFrame.duration < composition.duration()) {
audioFrame = reader.readNextSample();
playAudio(audioFrame);
}
iOS:
if ([reader isEmpty]) {
// If [reader isEmpty] returns true, it indicates that the composition has no audio data.
return;
}
PAGAudioSample* audioFrame = nil;
while ((audioFrame = [reader readNextSample]) != null
&& audioFrame.timestamp + audioFrame.duration < composition.duration) {
audioFrame = reader.readNextSample();
dealWithAudio(audioFrame);
}