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 adjusting the audio volume, ensure that you have implemented the basic real-time communication functions in your project. For details, see the following quickstart guides:
To implement a media player in your project, refer to the following steps.
After the AgoraRtcEngineKit
initialization, create an AgoraRtcMediaPlayerProtocol
instance.
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
...
mediaPlayerKit = agoraKit.createMediaPlayer(with: self)
Implement extensions for media player callbacks.
extension MediaPlayerMain: AgoraRtcMediaPlayerDelegate {
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
}
}
}
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)
}
}
}
}
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)
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.
let option1 = AgoraRtcChannelMediaOptions()
...
option1.publishMediaPlayerId = .of((Int32)(mediaPlayerKit.getMediaPlayerId()))
Open a local or online media file.
mediaPlayerKit.open(url, startPos: 0)
Play a media file.
mediaPlayerKit.play()
didChangedToState
callback reporting AgoraMediaPlayerStateOpenCompleted
before calling this method to play a file.Stop the media player and destroy the allocated resources when the user leaves the channel.
mediaPlayerKit.stop()
agoraKit.destroyMediaPlayer(mediaPlayerKit)
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 MediaPlayer on GitHub that implements the media player function.