This page tells you how to use the media player APIs to play local or online media locally or to remote users in an Agora channel.
Before proceeding, ensure that you have implemented basic real-time functions in your project. See Start a Call or Start Interactive Live Streaming.
To implement a media player in your project, refer to the following steps.
After initialization, create an IMediaPlayer
object and call registerPlayerObserver
to register a media observer for the media player.
engine = RtcEngine.create(config);
...
mediaPlayer = engine.createMediaPlayer();
mediaPlayer.registerPlayerObserver(this);
Implement callbacks for the media player observer.
@Override
public void onPlayerStateChanged(io.agora.mediaplayer.Constants.MediaPlayerState mediaPlayerState, io.agora.mediaplayer.Constants.MediaPlayerError mediaPlayerError) {
Log.e(TAG, "onPlayerStateChanged mediaPlayerState " + mediaPlayerState);
Log.e(TAG, "onPlayerStateChanged mediaPlayerError " + mediaPlayerError);
if (mediaPlayerState.equals(PLAYER_STATE_OPEN_COMPLETED)) {
setMediaPlayerViewEnable(true);
} else if (mediaPlayerState.equals(PLAYER_STATE_IDLE) || mediaPlayerState.equals(PLAYER_STATE_PLAYBACK_COMPLETED) ) {
setMediaPlayerViewEnable(false);
}
}
@Override
public void onPositionChanged(long position) {
Log.e(TAG, "onPositionChanged position " + position);
if (playerDuration > 0) {
final int result = (int) ((float) position / (float) playerDuration * 100);
handler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(Long.valueOf(result).intValue());
}
});
}
}
@Override
public void onPlayerEvent(io.agora.mediaplayer.Constants.MediaPlayerEvent mediaPlayerEvent) {
Log.e(TAG, " onPlayerEvent mediaPlayerEvent " + mediaPlayerEvent);
}
Render the local media player view.
VideoCanvas videoCanvas = new VideoCanvas(surfaceView, Constants.RENDER_MODE_HIDDEN, Constants.VIDEO_MIRROR_MODE_AUTO,
Constants.VIDEO_SOURCE_MEDIA_PLAYER, mediaPlayer.getMediaPlayerId(), 0);
engine.setupLocalVideo(videoCanvas);
Before joining the channel, specify the media player ID and enable publishing media player tracks to share media to remote users in an Agora channel.
private ChannelMediaOptions options = new ChannelMediaOptions();
...
options.publishMediaPlayerId = mediaPlayer.getMediaPlayerId();
options.publishMediaPlayerAudioTrack = true;
options.publishMediaPlayerVideoTrack = true;
int res = engine.joinChannel(accessToken, channelId, 0, options);
Open a local or online media file.
mediaPlayer.open(url, 0);
Play a media file.
mediaPlayer.play();
onPlayerStateChanged
callback reporting PLAYER_STATE_OPEN_COMPLETED
before calling this method to play a file.Stop the media player and destroy the allocated resources when the user leaves the channel.
mediaPlayer.stop();
mediaPlayer.destroy();
mediaPlayer.unRegisterPlayerObserver(this);
This section includes in depth information about the methods you used in this page, and links to related pages.
Agora provides an open-source sample project on GitHub that implements the media player function.