The MediaPlayer Kit is a powerful player that supports playing local and online media resources. With this player, you can either locally play media resources or synchronously share currently playing media resources with remote users in the Agora channel.
Open the specified ports in Firewall Requirements if your network has a firewall.
A valid Agora account (Sign up for free) is necessary when sharing media resources to remote users.
Now, let's build an iOS project from scratch.
Open Xcode and click Create a new Xcode project.
Choose Single View App as the template and click Next.
Input the project information, such as the project name, team, organization name, and language, and click Next.
Note: If you haven't added any team information, you will see an Add account... button. Click it, input your Apple ID, and click Next to add your team.
Choose the storage path of the project and click Create.
Connect your iOS device to your computer.
Go to the TARGETS > Project Name > Signing & Capabilities menu, choose Automatically manage signing, and then click Enable Automatic on the pop-up window.
Choose either of the following methods to integrate the MediaPlayer Kit into your project.
Method 1: Automatically integrate the MediaPlayer Kit with CocoaPods
Only applies to MediaPlayer Kit v1.1.2 and later.
Ensure that you have installed CocoaPods before the following steps. See the installation guide in Getting Started with CocoaPods.
In Terminal, go to the project path and run the pod init command to create a Podfile in the project folder.
Open the Podfile, delete all contents and input the following contents. Remember to change Your App to the target name of your project, and change version to the version of the MediaPlayer Kit which you want to integrate.
target 'Your App' do
pod 'AgoraMediaPlayer_iOS', '~> version'
endIn Terminal, run the pod update command to update the local libraries.
Run the pod install command to install the MediaPlayer Kit. Once you successfully install the Kit, it shows Pod installation complete! in Terminal, and you can see an xcworkspace file in the project folder.
Open the generated xcworkspace file in Xcode.
Method 2: Manually add the MediaPlayer Kit files
Go to Downloads, download the latest version of the MediaPlayer Kit, and unzip the download package.
Add the AgoraMediaPlayer.framework file in the libs folder to the project folder.
In Xcode, click TARGETS > Project Name > General > Frameworks, Libraries, and Embedded Content to change the status of AgoraMediaPlayer.framework to Embed & Sign.
Go to the TARGETS > Project Name > Build Phases > Link Binary with Libraries menu, and click + to add the following frameworks and libraries. To add the AgoraMediaPlayer.framework file, remember to click Add Other... after clicking +.
Add the following permissions in the info.plist file for device access according to your needs:
| Key | Type | Value |
|---|---|---|
| Privacy - Microphone Usage Description | String | To access the microphone, such as for a video call. |
| Privacy - Camera Usage Description | String | To access the camera, such as for a video call. |
Version requirements: 2.4.0 or later
Integration steps: See Integrate the Native SDK.
After integrating the MediaPlayer Kit, follow these steps to implement the local playback function.
Create a player instance
Create an instance of AgoraMediaPlayer.
To play different media resources simultaneously, you should create multiple instances.
Receive event callbacks
Override the AgoraMediaPlayerDelegate delegate method to receive the following event callbacks:
didChangedToPosition, which reports the current playback progress.didChangedToState, which reports the playback state change.didOccurEvent, which reports the result of a seek operation to a new playback position.didReceiveData, which occurs each time the player receives the media metadata.didReceiveAudioFrame, which occurs each time the player receives an audio frame.didReceiveVideoFrame, which occurs each time the player receives a video frame.By listening for these events, you can have more control over the playback process and enable your app to support data in custom formats (media metadata). If an exception occurs, you can use these event callbacks for troubleshooting.
Preparations for playback
Call the setView method in the AgoraMediaPlayer interface to set the render view of the player.
Call the setRenderMode method in the AgoraMediaPlayer interface to set the rendering mode of the player's view.
Call the open method in the AgoraMediaPlayer interface to open the media resource. The media resource path can be a network path or a local path. Both absolute and relative paths are supported.
Do not proceed until you receive the
didChangedToStatecallback reportingAgoraMediaPlayerStateOpenCompleted(2).
Call the play method in the AgoraMediaPlayer interface to play the media resource locally.
Adjust playback settings
You can call several other methods in the AgoraMediaPlayer interface to implement various playback settings:
Stop playback
stop method in the AgoraMediaPlayer interface to stop playback.view in the setView method to NULL to release the view.AgoraMediaPlayer instance.Sample code
_mediaPlayerKit = [[AgoraMediaPlayer alloc] initWithDelegate:self];
[_mediaPlayerKit setView:self.containerView];
[_mediaPlayerKit open:url startPos:0];
[_mediaPlayerKit play];
[_mediaPlayerKit stop];
[_mediaPlayerKit seekToPosition:value];
[_mediaPlayerKit adjustVolume:volume];
// Receives event callbacks.
- (void)AgoraMediaPlayer:(AgoraMediaPlayer *_Nonnull)playerKit
didChangedToState:(AgoraMediaPlayerState)state
error:(AgoraMediaPlayerError)error;
{
//todo
}
- (void)AgoraMediaPlayer:(AgoraMediaPlayer *_Nonnull)playerKit
didChangedToPosition:(NSInteger)position;
{
}
- (void)AgoraMediaPlayer:(AgoraMediaPlayer *_Nonnull)playerKit
didOccurEvent:(AgoraMediaPlayerEvent)event;
{
//todo
}
- (void)AgoraMediaPlayer:(AgoraMediaPlayer *_Nonnull)playerKit
didReceiveData:(NSString *)data
length:(NSInteger)length;
{
//todo
}
- (void)AgoraMediaPlayer:(AgoraMediaPlayer *_Nonnull)playerKit
didReceiveVideoFrame:(CVPixelBufferRef)
{
//todo
}
- (void)AgoraMediaPlayer:(AgoraMediaPlayer *_Nonnull)playerKit
didReceiveAudioFrame:(CMSampleBufferRef)
{
//todo
};
After integrating the MediaPlayer Kit, the Agora Native SDK, and the RtcChannelPublishPlugin, follow these steps to synchronously share media resources played by the local user to all the remote users in the Agora channel.
Instantiate required objects
AgoraMediaPlayer and RtcChannelPublishHelper objects.Enable the player to complete playback preparations
Register the player, audio, and video observer objects, and complete the preparations for playback. See Play media resources locally for details.
Do not proceed until you receive the
didChangedToStatecallback reportingAgoraMediaPlayerStatePlaying(3).
Enable the local user to join the channel by using the SDK
Refer to the RTC quickstart guide for details about how to enable the local user to join the LIVE_BROADCASTING channel in the role of BROADCASTER:
Call the setChannelProfile method to set the channel profile to LIVE_BROADCASTING.
Call the setClientRole method to set the local user role as the host.
Call the enableVideo method to enable the video module.
Call the joinChannel method to enable the local user to join the channel.
Do not proceed until you receive the
joinSuccessBlockordidJoinChannelcallback.
Start sharing by using the helper
Call the attachPlayerToRtc method to bundle the player with the Agora channel. And the playback window fully occupies the local user's view.
Call the publishVideo/publishAudio method to share (publish) the video/audio stream in the media resource to remote users in the Agora channel.
Call the adjustPublishSignalVolume method to adjust the the playback volume heard by the remote user.
Cancel sharing by using the helper
Call the unpublishVideo/unpublishAudio method to unshare or unpublish the video/audio stream in the media resource.
Call the detachPlayerFromRtc method to unbind the player from the Agora channel.
(Optional) Call the setVideoSource method to switch the player's window back to the local user's view and enable the remote users to see the local user again.
leaveChannel method to cancel the media stream being shared, otherwise abnormal behaviors occur when the local user rejoins the channel:
Sample code
_rtcEnginekit = [AgoraRtcEngineKit sharedEngineWithAppId:@"YOUR_APPID" delegate:self];
[_rtcEnginekit setChannelProfile:AgoraChannelProfileLiveBroadcasting];
[_rtcEnginekit setClientRole:AgoraClientRoleBroadcaster];
[_rtcEnginekit enableVideo];
[_rtcEnginekit joinChannelByToken:token channelId:channelid info:"" uid:"" joinSuccess:NULL];
[[AgoraRtcChannelPublishHelper shareInstance] attachPlayerToRtc:_mediaPlayerKitOC RtcEngine:_rtcEnginekit enableVideoSource:true];
[[AgoraRtcChannelPublishHelper shareInstance] publishAudio];
[[AgoraRtcChannelPublishHelper shareInstance] publishVideo];
[[AgoraRtcChannelPublishHelper shareInstance] detachPlayerFromRtc];
if(!_defaultCamera)
{
_defaultCamera = [[AgoraRtcDefaultCamera alloc] init];
}
[_rtcEngineKit setVideoSource:NULL];
[_rtcEngineKit setVideoSource:_defaultCamera];
The log file contains all the log events generated by the mediaplayer kit during runtime. The path of the log file is App Sandbox/Library/caches/agoraplayer.log.
See the API documentation.