1 Star 0 Fork 5.3K

Almeida / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
audio-and-video-playback-development-guide.md 28.17 KB
一键复制 编辑 原始数据 按行查看 历史
wenjun 提交于 2020-09-08 10:08 . add OpenHarmony 1.0 baseline

Audio and Video Playback Development Guide

When to Use

Audio and video playback is a process in which audio and video files or audio and video stream data is decoded and played by using an output device, and a playback task is managed.

Available APIs

The audio and video playback APIs are described as follows. For details about the APIs, see the API reference document.

Table 1 Audio and Video Playback APIs

API

Function

Description

Player

static Player *CreatePlayer();

Creates a Player instance.

Player

int32_t SetSource(const Source &source);

Set playback source

Player

int32_t Prepare();

Prepares the playback environment and buffers media data.

Player

int32_t Play();

Starts playback.

Player

bool IsPlaying()

Determines whether the playback is in progress.

Player

int32_t Pause();

Pauses playback.

Player

int32_t Stop();

Stops playback.

Player

int32_t Rewind(int_64 mSeconds, int32_t mode);

Change the playback position.

Player

int32_t SetVolume(float leftVolume, float rightVolume);

Sets the volume of the audio-left and audio-right channels.

Player

int32_t SetVideoSurface(Surface *surface)

Setting the Playback Window

Player

int32_t EnableSingleLooping(bool loop)

Single-song repeat

Player

bool IsSingleLooping();

Determines whether to play the video cyclically.

Player

int32_t GetCurrentTime(int64_t &time) const;

Obtains the current playback duration.

Player

int32_t GetDuration(int64_t &duration) const;

Obtains the total playback duration.

Player

int32_t GetVideoWidth(int32_t &videoWidth);

Obtains the width of a video source.

Player

int32_t GetVideoHeight(int32_t &videoHeight);

Obtains the height of a video source.

Player

int32_t Reset();

Restores the player to the initial state.

Player

int32_t Release();

Releasing Player Resources

Player

void SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb);

Sets the playback callback function.

Source

Source(const std::string& uri);

Creating a Source Instance Based on the URI

Source

Source(const std::string &uri, const std::map<std::string, std::string> &header);

Creating a Source Instance Based on the URI and URI Header

Source

Source(const std::shared_ptr<StreamSource> &stream, const Format &formats);

Creating a Source Instance Based on Flows

Source

SourceType GetSourceType() const;

Obtains the source type.

Source

const std::string &GetSourceUri() const;

Obtains the audio and video URIs.

Source

const std::map<std::string, std::string> &GetSourceHeader() const;

Obtains the audio and video URI headers.

Source

const std::shared_ptr<StreamSource> &GetSourceStream() const;

Obtaining Audio and Video Streams

Source

const Format &GetSourceStreamFormat() const;

Obtains the audio and video stream formats.

Format

bool PutIntValue(const std::string &key, int32_t value);

Sets the metadata of the integer type.

Format

bool PutLongValue(const std::string &key, int64_t value);

Sets the metadata of the long integer type.

Format

bool PutFloatValue(const std::string &key, float value);

Sets the metadata of the single-precision floating-point type.

Format

bool PutDoubleValue(const std::string &key, double value);

Sets the metadata of the double-precision floating-point type.

Format

bool PutStringValue(const std::string &key, const std::string &value);

Sets the metadata of the character string type.

Format

bool GetIntValue(const std::string &key, int32_t &value) const;

Obtains the metadata value of the integer type.

Format

bool GetLongValue(const std::string &key, int64_t &value) const;

Obtains the metadata value of the long integer type.

Format

bool GetFloatValue(const std::string &key, float &value) const;

Obtains the metadata value of the single-precision floating-point type.

Format

bool GetDoubleValue(const std::string &key, double &value) const;

Obtains the metadata value of the double-precision floating-point type.

Format

bool GetStringValue(const std::string &key, std::string &value) const;

Obtains the metadata value of the character string type.

Format

const std::map<std::string, FormatData *> &GetFormatMap() const;

Obtain the Map table of the format.

Format

bool CopyFrom(const Format &format);

Copies all content from a format instance.

Limitations and Constraints

When the input source is an audio or video stream, the playback progress cannot be controlled and the file duration cannot be obtained.

How to Develop

  1. Implement the PlayerCallback callback function for event processing.

    class TestPlayerCallback : public PlayerCallback{ 
        void OnPlayBackComplete() override 
        { 
    // The implementation code is used to process the event that the file playback is complete.
        } 
        void OnError(int32_t errorType, int32_t errorCode) override 
        { 
    // Implement the code processing error event.
        } 
        void OnInfo(int type, int extra) override 
        { 
    //Implement the code to process common events.
        } 
        void OnRewindToComplete() override 
        { 
    //Implement the event that the code processing progress control is complete.
        } 
    };
    
  2. Create a player instance, set the playback source, and start playback.

    Player *player = Player::CreatePlayer(); 
    std::shared_ptr<PlayerCallback> callback = std::make_shared<TestPlayerCallback>(); 
    player->SetPlayerCallback(callback);// Set player callback.
    std::string uri(filePath);// filePath is the local file path.
    Source source(uri);//Save the URI to the source instance.
    player->SetSource(source);//Set the source to the player.
    player->SetVideoSurface(surface);//Set the playback window.
    player->Prepare(); // Prepare for the playback.
    player->Play(); //Starts playback.
  3. Perform playback control based on the scenario.

    player->SetVolume(lvolume, rvolume);//Set the left and right sound channels.
    player->EnableSingleLooping(true);// Set cyclic playback.
    player->Pause(); //Pause.
    player->Play(); //Continue the playback.
  4. Release resources after the playback task is complete.

    player->Stop(); //Stop the playback.
    player->Release(); // Release resources.
1
https://gitee.com/laudrup/docs.git
git@gitee.com:laudrup/docs.git
laudrup
docs
docs
master

搜索帮助