Audio/Video Playback Development
When to Use
Audio and video playback is a process to decode audio and video files or stream data and play them by an output device. During the process, playback tasks are managed.
Available APIs
The following table describes APIs available for audio and video playback.
Table 1 APIs available for media playback
Limitations and Constraints
When the input source is a media stream, the playback progress cannot be controlled and the file duration cannot be obtained.
How to Develop
-
Implement a PlayerCallback function, register the callback via SetPlayerCallback for event processing.
class TestPlayerCallback : public PlayerCallback{ void OnPlaybackComplete() override { // Process the event that the file playback is complete. } void OnError(int32_t errorType, int32_t errorCode) override { // Process the error event. } void OnInfo(int type, int extra) override { // Process a common event. } void OnRewindToComplete() override { // Process the event that playback position is changed. } };
-
Create a Player instance, set the playback source, and start playback.
Player *player = new Player(); std::shared_ptr<PlayerCallback> callback = std::make_shared<TestPlayerCallback>(); player->SetPlayerCallback(callback);// Set a player callback. std::string uri(filePath);// filePath is a local file path. Source source(uri);// Create a Source instance and save the URI to the instance. player->SetSource(source);// Set the Source instance to the player. player->Prepare(); // Prepare for the playback. player->SetVideoSurface(surface);// Set the playback surface. player->Play(); // Start playback.
-
Control the playback as needed.
player->SetVolume(lvolume, rvolume);// Set volume for left and right channels. player->EnableSingleLooping(true);// Enable loop playback. player->Pause(); // Pause playback. player->Play(); // Resume playing.
-
Release resources after the playback is complete.
player->Stop(); // Stop playback. player->Release(); // Release resources.