IMediaPlayer
This class provides media player functions and supports multiple instances.
AdjustPlayoutVolume
Adjusts the local playback volume.
public abstract int AdjustPlayoutVolume(int volume);
Call timing
This method can be called either before or after joining the channel.
Restrictions
None.
Parameters
- volume
- The local playback volume, which ranges from 0 to 100:
- 0: Mute.
- 100: (Default) The original volume.
Returns
- 0: Success.
- < 0: Failure.
AdjustPublishSignalVolume
Adjusts the volume of the media file for publishing.
public abstract int AdjustPublishSignalVolume(int volume);
After connected to the Agora server, you can call this method to adjust the volume of the media file heard by the remote user.
Call timing
This method can be called either before or after joining the channel.
Restrictions
None.
Parameters
- volume
- The volume, which ranges from 0 to 400:
- 0: Mute.
- 100: (Default) The original volume.
- 400: Four times the original volume (amplifying the audio signals by four times).
Returns
- 0: Success.
- < 0: Failure.
Dispose
Releases all the resources occupied by the media player.
public abstract void Dispose();
GetDuration
Gets the duration of the media resource.
public abstract int GetDuration(ref Int64 duration);
Parameters
- duration
- An output parameter. The total duration (ms) of the media file.
Returns
- 0: Success.
- < 0: Failure.
GetMediaPlayerCacheManager
Gets one IMediaPlayerCacheManager instance.
public abstract IMediaPlayerCacheManager GetMediaPlayerCacheManager();
Before calling any APIs in the IMediaPlayerCacheManager class, you need to call this method to get a cache manager instance of a media player.
Call timing
Make sure the IRtcEngine is initialized before you call this method.
Restrictions
The cache manager is a singleton pattern. Therefore, multiple calls to this method returns the same instance.
Returns
The IMediaPlayerCacheManager instance.
GetId
Gets the ID of the media player.
public abstract int GetId();
Returns
- Success. The ID of the media player.
- < 0: Failure.
GetMute
Reports whether the media resource is muted.
public abstract int GetMute(ref bool muted);
Parameters
- muted
- An output parameter. Whether the media file is muted:
true
: The media file is muted.false
: The media file is not muted.
Returns
- 0: Success.
- < 0: Failure.
GetPlaySrc
Gets the path of the media resource being played.
public abstract string GetPlaySrc();
Returns
The path of the media resource being played.
GetPlayoutVolume
Gets the local playback volume.
public abstract int GetPlayoutVolume(ref int volume);
Parameters
- volume
- An output parameter. The local playback volume, which ranges from 0 to 100:
- 0: Mute.
- 100: (Default) The original volume.
Returns
- 0: Success.
- < 0: Failure.
GetPlayPosition
Gets current local playback progress.
public abstract int GetPlayPosition(ref Int64 pos);
Parameters
- pos
- The playback position (ms) of the audio effect file.
Returns
- Returns the current playback progress (ms) if the call succeeds.
- < 0: Failure. See MEDIA_PLAYER_REASON.
GetPublishSignalVolume
Gets the volume of the media file for publishing.
public abstract int GetPublishSignalVolume(ref int volume);
Parameters
- volume
- An output parameter. The remote playback volume.
Returns
- 0: Success.
- < 0: Failure.
GetState
Gets current playback state.
public abstract MEDIA_PLAYER_STATE GetState();
Returns
The current playback state. See MEDIA_PLAYER_STATE.
GetStreamCount
Gets the number of the media streams in the media resource.
public abstract int GetStreamCount(ref Int64 count);
Details
Parameters
- count
- An output parameter. The number of the media streams in the media resource.
Returns
- 0: Success.
- < 0: Failure. See MEDIA_PLAYER_REASON.
GetStreamInfo
Gets the detailed information of the media stream.
public abstract int GetStreamInfo(Int64 index, ref PlayerStreamInfo info);
Call timing
Call this method after calling GetStreamCount.
Restrictions
None.
Parameters
- index
- The index of the media stream. This parameter needs to be less than the count parameter of GetStreamCount.
- info
- An output parameter. The detailed information of the media stream. See PlayerStreamInfo.
Returns
- 0: Success.
- < 0: Failure.
InitEventHandler
Adds callback event for media player.
public abstract int InitEventHandler(IMediaPlayerSourceObserver engineEventHandler);
Parameters
- engineEventHandler
- Callback events to be added. See IMediaPlayerSourceObserver.
Returns
- 0: Success.
- < 0: Failure.
Mute
Sets whether to mute the media file.
public abstract int Mute(bool muted);
Call timing
You can call this method either before or after joining a channel.
Restrictions
None.
Parameters
- muted
- Whether to mute the media file:
true
: Mute the media file.false
: (Default) Unmute the media file.
Returns
- 0: Success.
- < 0: Failure.
Open
Opens the media resource.
public abstract int Open(string url, Int64 startPos);
This method is called asynchronously.
Call timing
This method can be called either before or after joining the channel.
Restrictions
None.
Parameters
- url
- The path of the media file. Both local path and online path are supported.
- startPos
- The starting position (ms) for playback. Default value is 0.
Returns
- 0: Success.
- < 0: Failure.
OpenWithMediaSource
Opens a media file and configures the playback scenarios.
public abstract int OpenWithMediaSource(MediaSource source);
This method supports opening media files of different sources, including a custom media source, and allows you to configure the playback scenarios.
Call timing
You can call this method either before or after joining a channel.
Restrictions
This method is called asynchronously. If you need to play a media file, make sure you receive the OnPlayerSourceStateChanged callback reporting PLAYER_STATE_OPEN_COMPLETED before calling the Play method to play the file.
Parameters
- source
- Media resources. See MediaSource.
Returns
- 0: Success.
- < 0: Failure.
Pause
Pauses the playback.
public abstract int Pause();
Call timing
You can call this method either before or after joining a channel.
Restrictions
None.
Returns
- 0: Success.
- < 0: Failure.
Play
Plays the media file.
public abstract int Play();
Call timing
- Call this method after calling Open or OpenWithMediaSource opening a media file and receiving a OnPlayerSourceStateChanged callback reporting the status as PLAYER_STATE_OPEN_COMPLETED.
- Call the method after calling Seek.
Restrictions
None.
Returns
- 0: Success.
- < 0: Failure.
PlayPreloadedSrc
Plays preloaded media resources.
public abstract int PlayPreloadedSrc(string src);
Details
After calling the PreloadSrc method to preload the media resource into the playlist, you can call this method to play the preloaded media resource. After calling this method, if you receive the OnPlayerSourceStateChanged callback which reports the PLAYER_STATE_PLAYING state, the playback is successful.
If you want to change the preloaded media resource to be played, you can call this method again and specify the URL of the new media resource that you want to preload. If you want to replay the media resource, you need to call PreloadSrc to preload the media resource to the playlist again before playing. If you want to clear the playlist, call the Stop method.
If you call this method when playback is paused, this method does not take effect until playback is resumed.
Parameters
- src
- The URL of the media resource in the playlist must be consistent with the src set by the PreloadSrc method; otherwise, the media resource cannot be played.
Returns
- 0: Success.
- < 0: Failure.
PreloadSrc
Preloads a media resource.
public abstract int PreloadSrc(string src, Int64 startPos);
Details
You can call this method to preload a media resource into the playlist. If you need to preload multiple media resources, you can call this method multiple times.
If the preload is successful and you want to play the media resource, call PlayPreloadedSrc; if you want to clear the playlist, call Stop.
- Before calling this method, ensure that you have called Open or OpenWithMediaSource to open the media resource successfully.
- Agora does not support preloading duplicate media resources to the playlist. However, you can preload the media resources that are being played to the playlist again.
Parameters
- src
- The URL of the media resource.
- startPos
- The starting position (ms) for playing after the media resource is preloaded to the playlist. When preloading a live stream, set this parameter to 0.
Returns
- 0: Success.
- < 0: Failure.
RegisterAudioFrameObserver [1/2]
Registers a PCM audio frame observer object.
public abstract int RegisterAudioFrameObserver(IAudioPcmFrameSink observer);
Details
You need to implement the IAudioPcmFrameSink class in this method and register callbacks according to your scenarios. After you successfully register the video frame observer, the SDK triggers the registered callbacks each time a video frame is received.
Parameters
- observer
- The audio frame observer, reporting the reception of each audio frame. See IAudioPcmFrameSink.
Returns
- 0: Success.
- < 0: Failure.
RegisterAudioFrameObserver [2/2]
Registers an audio frame observer object.
public abstract int RegisterAudioFrameObserver(IAudioPcmFrameSink observer, RAW_AUDIO_FRAME_OP_MODE_TYPE mode);
Parameters
- observer
-
The audio frame observer, reporting the reception of each audio frame. See IAudioPcmFrameSink.
- mode
-
The use mode of the audio frame. See RAW_AUDIO_FRAME_OP_MODE_TYPE.
Returns
- 0: Success.
- < 0: Failure.
Resume
Resumes playing the media file.
public abstract int Resume();
Returns
- 0: Success.
- < 0: Failure.
Seek
Seeks to a new playback position.
public abstract int Seek(Int64 newPos);
- If you call Seek after the playback has completed (upon receiving callback OnPlayerSourceStateChanged reporting playback status as PLAYER_STATE_PLAYBACK_COMPLETED or PLAYER_STATE_PLAYBACK_ALL_LOOPS_COMPLETED), the SDK will play the media file from the specified position. At this point, you will receive callback OnPlayerSourceStateChanged reporting playback status as PLAYER_STATE_PLAYING.
- If you call Seek while the playback is paused, upon successful call of this method, the SDK will seek to the specified position. To resume playback, call Resume orPlay .
Call timing
You can call this method either before or after joining a channel.
Restrictions
None.
Parameters
- newPos
- The new playback position (ms).
Returns
- 0: Success.
- < 0: Failure.
SelectAudioTrack [2/2]
Selects the audio track used during playback.
public abstract int SelectAudioTrack(int index);
Details
After getting the track index of the audio file, you can call this method to specify any track to play. For example, if different tracks of a multi-track file store songs in different languages, you can call this method to set the playback language.
Parameters
- index
- The index of the audio track.
Returns
- 0: Success.
- < 0: Failure.
SetAudioPitch
Sets the pitch of the current media resource.
public abstract int SetAudioPitch(int pitch);
Details
Parameters
- pitch
- Sets the pitch of the local music file by the chromatic scale. The default value is 0, which means keeping the original pitch. The value ranges from -12 to 12, and the pitch value between consecutive values is a chromatic value. The greater the absolute value of this parameter, the higher or lower the pitch of the local music file.
Returns
- 0: Success.
- < 0: Failure.
SetAudioDualMonoMode
Sets the channel mode of the current audio file.
public abstract int SetAudioDualMonoMode(AUDIO_DUAL_MONO_MODE mode);
Details
In a stereo music file, the left and right channels can store different audio data. According to your needs, you can set the channel mode to original mode, left channel mode, right channel mode, or mixed channel mode. For example, in the KTV scenario, the left channel of the music file stores the musical accompaniment, and the right channel stores the singing voice. If you only need to listen to the accompaniment, call this method to set the channel mode of the music file to left channel mode; if you need to listen to the accompaniment and the singing voice at the same time, call this method to set the channel mode to mixed channel mode.
- Call this method after calling Open.
- This method only applies to stereo audio files.
Parameters
- mode
- The channel mode. See AUDIO_DUAL_MONO_MODE.
Returns
- 0: Success.
- < 0: Failure.
SetLoopCount
Sets the loop playback.
public abstract int SetLoopCount(int loopCount);
Details
If you want to loop, call this method and set the number of the loops.
When the loop finishes, the SDK triggers OnPlayerSourceStateChanged and reports the playback state as PLAYER_STATE_PLAYBACK_ALL_LOOPS_COMPLETED.
Parameters
- loopCount
- The number of times the audio effect loops:
- ≥0: Number of times for playing. For example, setting it to 0 means no loop playback, playing only once; setting it to 1 means loop playback once, playing a total of twice.
- -1: Play the audio file in an infinite loop.
Returns
- 0: Success.
- < 0: Failure.
SetPlaybackSpeed
Sets the channel mode of the current audio file.
public abstract int SetPlaybackSpeed(int speed);
Details
Call this method after calling Open.
Parameters
- speed
- The playback speed. Agora recommends that you set this to a value between 30 and 400, defined as follows:
- 30: 0.3 times the original speed.
- 100: The original speed.
- 400: 4 times the original speed.
Returns
- 0: Success.
- < 0: Failure.
SetPlayerOption [1/2]
Sets media player options.
public abstract int SetPlayerOption(string key, int value);
The media player supports setting options through key and value.
The difference between this method and setPlayerOption [2/2] is that the value parameter of this method is of type Int, while the value of setPlayerOption [2/2] is of type String. These two methods cannot be used together.
Applicable scenarios
Scenarios that require technical previews or special customization features. In general, you do not need to call this method; you can simply use the default options provided by the media player.
Call timing
Call this method before the Open or OpenWithMediaSource method.
Restrictions
None.
Parameters
- key
- The key of the option.
- value
- The value of the key.
Returns
- 0: Success.
- < 0: Failure.
setPlayerOption [2/2]
Sets media player options.
public abstract int SetPlayerOption(string key, string value);
The media player supports setting options through key and value.
The difference between this method and SetPlayerOption [1/2] is that the value parameter of this method is of type String, while the value of SetPlayerOption [1/2] is of type String. These two methods cannot be used together.
Applicable scenarios
Scenarios that require technical previews or special customization features. In general, you do not need to call this method; you can simply use the default options provided by the media player.
Call timing
Call this method before the Open or OpenWithMediaSource method.
Restrictions
None.
Parameters
- key
- The key of the option.
- value
- The value of the key.
Returns
- 0: Success.
- < 0: Failure.
SetRenderMode
Sets the render mode of the media player.
public abstract int SetRenderMode(RENDER_MODE_TYPE renderMode);
Parameters
- renderMode
-
Sets the render mode of the view. See RENDER_MODE_TYPE.
Returns
- 0: Success.
- < 0: Failure.
SetSpatialAudioParams
Enables or disables the spatial audio effect for the media player.
public abstract int SetSpatialAudioParams(SpatialAudioParams spatial_audio_params);
Details
After successfully setting the spatial audio effect parameters of the media player, the SDK enables the spatial audio effect for the media player, and the local user can hear the media resources with a sense of space.
If you need to disable the spatial audio effect for the media player, set the params parameter to null.
Parameters
- spatial_audio_params
- The spatial audio effect parameters of the media player. See SpatialAudioParams.
Returns
- 0: Success.
- < 0: Failure.
SetView
Sets the view.
public abstract int SetView();
Call timing
You can call this method either before or after joining a channel.
Restrictions
None.
Returns
- 0: Success.
- < 0: Failure.
Stop
Stops playing the media track.
public abstract int Stop();
After calling this method to stop playback, if you want to play again, you need to call Open or OpenWithMediaSource to open the media resource.
Call timing
Call this method after Play.
Restrictions
None.
Returns
- 0: Success.
- < 0: Failure.
SwitchSrc
Switches the media resource being played.
public abstract int SwitchSrc(string src, bool syncPts = true);
Details
- When the network is poor, the media resource to be played is switched to a media resource address with a lower bitrate.
- When the network is good, the media resource to be played is switched to a media resource address with a higher bitrate.
After calling this method, if you receive the OnPlayerEvent callback report the PLAYER_EVENT_SWITCH_COMPLETE event, the switching is successful. If the switching fails, the SDK will automatically retry 3 times. If it still fails, you will receive the OnPlayerEvent callback reporting the PLAYER_EVENT_SWITCH_ERROR event indicating an error occurred during media resource switching.
- Ensure that you call this method after Open.
- To ensure normal playback, pay attention to the following when calling this method:
- Do not call this method when playback is paused.
- Do not call the Seek method during switching.
- Before switching the media resource, make sure that the playback position does not exceed the total duration of the media resource to be switched.
Parameters
- src
- The URL of the media resource.
- syncPts
- Whether to synchronize the playback position (ms) before and after the switch:
true
: Synchronize the playback position before and after the switch.false
: (Default) Do not synchronize the playback position before and after the switch.
Returns
- 0: Success.
- < 0: Failure.
UnloadSrc
Unloads media resources that are preloaded.
public abstract int UnloadSrc(string src);
Details
This method cannot release the media resource being played.
Parameters
- src
- The URL of the media resource.
Returns
- 0: Success.
- < 0: Failure.
UnregisterAudioFrameObserver
Unregisters an audio frame observer.
public abstract int UnregisterAudioFrameObserver();
Returns
- 0: Success.
- < 0: Failure.