Skip to main content

You are viewing Agora Docs forBetaproducts and features. Switch to Docs

Android
iOS
macOS
Web
Windows
Electron
Flutter
React Native
React JS
Unity
Unreal Engine
Unreal (Blueprint)

Stream media to a channel

Playing media files during online business presentations, educational sessions, or casual meetups heightens user engagement. Video SDK enables you to add media playing functionality to your app.

This page shows you how to use media player-related APIs to play local or online media resources with remote users in Video Calling channels.

Understand the tech

To play a media file in a channel, you open the file using a media player instance. When the file is ready to be played, you set up the local video container to display the media player output. You update channel media options to start publishing the media player stream, and stop publishing the camera and microphone streams. The remote user sees the camera and microphone streams of the media publishing user replaced by media streams.

Media player

Prerequisites

Ensure that you have implemented the SDK quickstart in your project.

Implement the logic

To implement a media player in your app, follow these steps:

  1. After initializing an RtcEngine instance, create an IMediaPlayer object and register a player observer by calling the registerPlayerObserver method.


    _6
    mRtcEngine = RtcEngine.create(config);
    _6
    _6
    // Create a media player object
    _6
    mediaPlayer = mRtcEngine.createMediaPlayer();
    _6
    // Register a player observer
    _6
    mediaPlayer.registerPlayerObserver(this);

  2. Implement the callbacks for the media player observer. Observe the player's state through the onPlayerStateChanged callback, get the current media file's playback progress through onPositionChanged, and handle player events through the onPlayerEvent callback.


    _32
    @Override
    _32
    // Observe the player's state
    _32
    public void onPlayerStateChanged(io.agora.mediaplayer.Constants.MediaPlayerState mediaPlayerState, io.agora.mediaplayer.Constants.MediaPlayerError mediaPlayerError) {
    _32
    Log.e(TAG, "onPlayerStateChanged mediaPlayerState " + mediaPlayerState);
    _32
    Log.e(TAG, "onPlayerStateChanged mediaPlayerError " + mediaPlayerError);
    _32
    if (mediaPlayerState.equals(PLAYER_STATE_OPEN_COMPLETED)) {
    _32
    setMediaPlayerViewEnable(true);
    _32
    } else if (mediaPlayerState.equals(PLAYER_STATE_IDLE) || mediaPlayerState.equals(PLAYER_STATE_PLAYBACK_COMPLETED)) {
    _32
    setMediaPlayerViewEnable(false);
    _32
    }
    _32
    }
    _32
    _32
    @Override
    _32
    // Observe the current playback progress
    _32
    public void onPositionChanged(long position) {
    _32
    Log.e(TAG, "onPositionChanged position " + position);
    _32
    if (playerDuration > 0) {
    _32
    final int result = (int) ((float) position / (float) playerDuration * 100);
    _32
    handler.post(new Runnable() {
    _32
    @Override
    _32
    public void run() {
    _32
    progressBar.setProgress(Long.valueOf(result).intValue());
    _32
    }
    _32
    });
    _32
    }
    _32
    }
    _32
    _32
    @Override
    _32
    // Observe player events
    _32
    public void onPlayerEvent(io.agora.mediaplayer.Constants.MediaPlayerEvent mediaPlayerEvent) {
    _32
    Log.e(TAG, " onPlayerEvent mediaPlayerEvent " + mediaPlayerEvent);
    _32
    }

  3. Call setupLocalVideo to render the local media player view.


    _3
    VideoCanvas videoCanvas = new VideoCanvas(surfaceView, Constants.RENDER_MODE_HIDDEN, Constants.VIDEO_MIRROR_MODE_AUTO,
    _3
    Constants.VIDEO_SOURCE_MEDIA_PLAYER, mediaPlayer.getMediaPlayerId(), 0);
    _3
    mRtcEngine.setupLocalVideo(videoCanvas);

  4. When joining a channel, use ChannelMediaOptions to set the media player ID, publish media player audio and video, and share media resources with remote users in the channel.


    _9
    private ChannelMediaOptions options = new ChannelMediaOptions();
    _9
    _9
    // Set up options
    _9
    options.publishMediaPlayerId = mediaPlayer.getMediaPlayerId();
    _9
    options.publishMediaPlayerAudioTrack = true;
    _9
    options.publishMediaPlayerVideoTrack = true;
    _9
    _9
    // Join the channel
    _9
    int res = mRtcEngine.joinChannel(accessToken, channelId, 0, options);

  5. Use the open method to open a local or online media file.


    _1
    mediaPlayer.open(url, 0);

  6. Call the play method to play the media file.


    _1
    mediaPlayer.play();

    Caution

    Call the play method to play the media file only after receiving the onPlayerStateChanged callback reporting the player state as PLAYER_STATE_OPEN_COMPLETED.

  7. When a user leaves the channel, call stop to stop playback, destroy to destroy the media player, unRegisterPlayerObserver to unregister the player observer, and release allocated resources.


    _3
    mediaPlayer.stop();
    _3
    mediaPlayer.destroy();
    _3
    mediaPlayer.unRegisterPlayerObserver(this);

Reference

This section contains information that completes the information in this page, or points you to documentation that explains other aspects to this product.

Supported formats and protocols

The media player supports the following media formats and protocols:

Video encoding formats

  • H.263, H.264, H.265, MPEG-4, MPEG-2, RMVB, Theora, VP3, VP8, AVS, WMV

Audio coding formats

  • WAV, MP2, MP3, AAC, OPUS, FLAC, Vorbis, AMR-NB, AMR-WB, WMA v1, WMA v2

Container formats

  • WAV, FLAC, OGG, MOV, ASF, FLV, MP3, MP4, MPEG-TS, Matroska (MKV), AVI, ASS, CONCAT, DTS, AVS

Supported protocols

  • HTTP, HTTPS, RTMP, HLS, RTP, RTSP

Sample project

Agora provides an open source sample project MediaPlayer on GitHub. Download it or view the source code for a more detailed example.

API reference

Video Calling