Stream media to a channel
Updated
Play local or online media files locally or to remote users in an Agora channel.
Playing media files during online business presentations, educational sessions, or casual meetups heightens user engagement. RTC 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 flow
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:
-
After initializing an instance of
AgoraRtcEngineKit, create amediaPlayerKitobject.// Initialize AgoraRtcEngineKit and create a mediaPlayerKit object. agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self) // Create mediaPlayerKit object mediaPlayerKit = agoraKit.createMediaPlayer(with: self) -
Implement media player callbacks. Observe the status of the player through callbacks and obtain the playback progress of the current media file.
extension MediaPlayerMain: AgoraRtcMediaPlayerDelegate { // Observe the state of the player func agoraRtcMediaPlayer(_ playerKit: AgoraRtcMediaPlayerProtocol, didChangedTo state: AgoraMediaPlayerState, error: AgoraMediaPlayerError) { LogUtils.log(message: "Player RTC channel publish helper state changed to: \(state.rawValue), error: \(error.rawValue)", level: .info) DispatchQueue.main.async { [weak self] in guard let weakself = self else { return } switch state { case .failed: weakself.showAlert(message: "Media player error: \(error.rawValue)") break case .openCompleted: let duration = weakself.mediaPlayerKit.getDuration() weakself.playerControlStack.isHidden = false weakself.playerDurationLabel.text = "\(String(format: "%02d", duration / 60)) : \(String(format: "%02d", duration % 60))" weakself.playerProgressSlider.setValue(0, animated: true) break case .stopped: weakself.playerControlStack.isHidden = true weakself.playerProgressSlider.setValue(0, animated: true) weakself.playerDurationLabel.text = "00 : 00" break default: break } } } // Observe the current playback progress func agoraRtcMediaPlayer(_ playerKit: AgoraRtcMediaPlayerProtocol, didChangedToPosition position: Int) { let duration = Float(mediaPlayerKit.getDuration() * 1000) var progress: Float = 0 var left: Int = 0 if duration > 0 { progress = Float(mediaPlayerKit.getPosition()) / duration left = Int((mediaPlayerKit.getDuration() * 1000 - mediaPlayerKit.getPosition()) / 1000) } DispatchQueue.main.async { [weak self] in guard let weakself = self else { return } weakself.playerDurationLabel.text = "\(String(format: "%02d", left / 60)) : \(String(format: "%02d", left % 60))" if !weakself.playerProgressSlider.isTouchInside { weakself.playerProgressSlider.setValue(progress, animated: true) } } } } -
Call the
setupLocalVideomethod to render the local media player view.mediaPlayerKit.setView(localVideo.videoView) let videoCanvas = AgoraRtcVideoCanvas() videoCanvas.view = localVideo.videoView videoCanvas.renderMode = .hidden videoCanvas.sourceType = .mediaPlayer videoCanvas.sourceId = mediaPlayerKit.getMediaPlayerId() agoraKit.setupLocalVideo(videoCanvas) -
When joining a channel using
joinChannelByToken, set the media player ID, publish media player's audio and video, and share media resources with remote users in the channel through themediaOptionsparameter.let option1 = AgoraRtcChannelMediaOptions() // ... option1.publishMediaPlayerId = .of((Int32)(mediaPlayerKit.getMediaPlayerId())) -
Use the
openmethod to open a local or online media file.mediaPlayerKit.open(url, startPos: 0) -
Call the
playmethod to play the media file.mediaPlayerKit.play()Call the
playmethod to play the media file only after receiving thedidChangedToStatecallback reporting the player state asAgoraMediaPlayerStateOpenCompleted. -
When a user leaves the channel, call
stopto stop playback, anddestroyMediaPlayerto release resources.mediaPlayerKit.stop() agoraKit.destroyMediaPlayer(mediaPlayerKit)
Reference
This section contains content that completes the information on 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.
