harmony 鸿蒙Audio/Video Playback Development

  • 2022-08-09
  • 浏览 (700)

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

API

Function

Description

Player

int32_t SetSource(const Source &source);

Sets the playback source for the player.

Player

int32_t Prepare();

Prepares the playback environment.

Player

int32_t Play();

Starts playback.

Player

bool IsPlaying()

Checks whether the player is playing.

Player

int32_t Pause();

Pauses playback.

Player

int32_t Stop();

Stops playback.

Player

int32_t Rewind(int_64 mSeconds, int32_t mode);

Changes the playback position.

Player

int32_t SetVolume(float leftVolume, float rightVolume);

Sets player volume, including volume of left and right channels.

Player

int32_t SetVideoSurface(Surface *surface)

Sets a surface for video playback.

Player

int32_t EnableSingleLooping(bool loop)

Enables loop playback.

Player

bool IsSingleLooping();

Checks whether loop playback is enabled.

Player

int32_t GetCurrentTime(int64_t &time) const;

Obtains the current playback duration, accurate to millisecond.

Player

int32_t GetDuration(int64_t &duration) const;

Obtains the total duration of media files, in milliseconds.

Player

int32_t GetVideoWidth(int32_t &videoWidth);

Obtains the width of the video.

Player

int32_t GetVideoHeight(int32_t &videoHeight);

Obtains the height of the video.

Player

int32_t Reset();

Restores the player to the initial state.

Player

int32_t Release();

Releases player resources.

Player

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

Sets a player callback.

Source

Source(const std::string& uri);

A constructor used to create a Source instance based on a specified URI.

Source

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

A constructor used to create a Source instance based on the stream source and format information.

Source

SourceType GetSourceType() const;

Obtains the source type.

Source

const std::string &GetSourceUri() const;

Obtains the media source URI.

Source

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

Obtains information about the media source stream.

Source

const Format &GetSourceStreamFormat() const;

Obtains the media source stream format.

Format

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

Sets metadata of the integer type.

Format

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

Sets metadata of the long integer type.

Format

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

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

Format

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

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

Format

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

Sets metadata of the 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 string type.

Format

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

Obtains the metadata map.

Format

bool CopyFrom(const Format &format);

Sets all metadata to a specified format.

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

  1. 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.
        } 
    };
    
    
    
  2. 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.
    
  3. 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.
    
  4. Release resources after the playback is complete.

    player->Stop(); // Stop playback.
    player->Release(); // Release resources.
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Subsystems

harmony 鸿蒙AI Framework Development Guide

harmony 鸿蒙NNRt Access Adaptation

harmony 鸿蒙Application Privilege Configuration

harmony 鸿蒙Development Example

harmony 鸿蒙Setting Up a Development Environment

harmony 鸿蒙Development Guidelines

harmony 鸿蒙Application Framework Overview

harmony 鸿蒙ArkCompiler Development

harmony 鸿蒙Custom Window Title Bar Development

0  赞