@@ -9,8 +9,8 @@ import UIKit
99import AGEVideoLayout
1010import AgoraRtcKit
1111
12- //TODO
13- //#if false
12+ let CAMERA_UID = UInt . random ( in : 1001 ... 2000 )
13+ let PLAYER_UID = UInt . random ( in : 2001 ... 3000 )
1414
1515class MediaPlayerEntry : UIViewController
1616{
@@ -51,6 +51,8 @@ class MediaPlayerMain: BaseViewController, UITextFieldDelegate {
5151
5252 var agoraKit : AgoraRtcEngineKit !
5353 var mediaPlayerKit : AgoraRtcMediaPlayerProtocol !
54+ var connectionId1 : UInt ?
55+ var connectionId2 : UInt ?
5456
5557 private var originY : CGFloat = 0
5658
@@ -126,7 +128,7 @@ class MediaPlayerMain: BaseViewController, UITextFieldDelegate {
126128 // prepare media player
127129 mediaPlayerKit = agoraKit. createMediaPlayer ( with: self )
128130 mediaPlayerKit. setView ( localVideo. videoView)
129- // mediaPlayerKit.setRenderMode(.fit)
131+ mediaPlayerKit. setRenderMode ( . fit)
130132
131133 playoutVolume. minimumValue = 0
132134 playoutVolume. maximumValue = 400
@@ -146,28 +148,29 @@ class MediaPlayerMain: BaseViewController, UITextFieldDelegate {
146148 let option1 = AgoraRtcChannelMediaOptions ( )
147149 option1. publishMediaPlayerVideoTrack = . of( true )
148150 option1. publishMediaPlayerAudioTrack = . of( true )
149- option1. publishAudioTrack = . of( true )
150- option1. autoSubscribeAudio = . of( true )
151+ option1. publishAudioTrack = . of( false )
152+ option1. autoSubscribeAudio = . of( false )
151153 option1. autoSubscribeVideo = . of( false )
152154 option1. clientRoleType = . of( ( Int32) ( AgoraClientRole . broadcaster. rawValue) )
153155 option1. publishMediaPlayerId = . of( ( Int32) ( mediaPlayerKit. getMediaPlayerId ( ) ) )
154- let connectionIdPointer = UnsafeMutablePointer< UInt> . allocate( capacity: MemoryLayout < UInt32 > . stride )
155- let result1 = agoraKit . joinChannelEx ( byToken : KeyCenter . Token , channelId : channelName , uid : 1001 , connectionId : connectionIdPointer, delegate : self , mediaOptions : option1 , joinSuccess : nil )
156-
156+ let connectionIdPointer = UnsafeMutablePointer< UInt> . allocate( capacity: 200 )
157+ connectionId1 = UInt ( connectionIdPointer. pointee )
158+ let result1 = agoraKit . joinChannelEx ( byToken : KeyCenter . Token , channelId : channelName , uid : PLAYER_UID , connectionId : connectionIdPointer , delegate : nil , mediaOptions : option1 , joinSuccess : nil )
157159 let option2 = AgoraRtcChannelMediaOptions ( )
158160 option2. publishCameraTrack = . of( true )
159- option2. autoSubscribeAudio = . of( false )
160- option2. autoSubscribeVideo = . of( false )
161+ option2. publishAudioTrack = . of( true )
162+ option2. autoSubscribeAudio = . of( true )
163+ option2. autoSubscribeVideo = . of( true )
161164 option2. clientRoleType = . of( ( Int32) ( AgoraClientRole . broadcaster. rawValue) )
162- let connectionIdPointer2 = UnsafeMutablePointer< UInt> . allocate( capacity: MemoryLayout< UInt32> . stride)
163- let result2 = agoraKit. joinChannelEx ( byToken: KeyCenter . Token, channelId: channelName, uid: 1002 , connectionId: connectionIdPointer2, delegate: self , mediaOptions: option2, joinSuccess: nil )
165+ let result2 = agoraKit. joinChannel ( byToken: KeyCenter . Token, channelId: channelName, uid: CAMERA_UID, mediaOptions: option2, joinSuccess: nil )
164166 if result1 != 0 && result2 != 0 {
165167 // Usually happens with invalid parameters
166168 // Error code description can be found at:
167169 // en: https://docs.agora.io/en/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
168170 // cn: https://docs.agora.io/cn/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
169171 self . showAlert ( title: " Error " , message: " joinChannel call failed: \( result1) , please check your params " )
170172 }
173+ connectionIdPointer. deallocate ( )
171174 }
172175
173176 @IBAction func doOpenMediaUrl( sender: UIButton ) {
@@ -189,18 +192,11 @@ class MediaPlayerMain: BaseViewController, UITextFieldDelegate {
189192 mediaPlayerKit. pause ( )
190193 }
191194
192- @IBAction func doSeek( sender: UISlider ) {
193- //let position = Int(sender.value * Float(mediaPlayerKit.getDuration()))
194- //let result = mediaPlayerKit.seek(toPosition: position)
195- }
196-
197195 @IBAction func doAdjustPlayoutVolume( sender: UISlider ) {
198- //AgoraRtcChannelPublishHelper.shareInstance().adjustPlayoutSignalVolume(Int32(Int(sender.value)))
199196 mediaPlayerKit. adjustPlayoutVolume ( Int32 ( Int ( sender. value) ) )
200197 }
201198
202199 @IBAction func doAdjustPublishVolume( sender: UISlider ) {
203- //AgoraRtcChannelPublishHelper.shareInstance().adjustPublishSignalVolume(Int32(Int(sender.value)))
204200 mediaPlayerKit. adjustPublishSignalVolume ( Int32 ( Int ( sender. value) ) )
205201 }
206202
@@ -250,6 +246,9 @@ extension MediaPlayerMain: AgoraRtcEngineDelegate {
250246 /// @param uid uid of remote joined user
251247 /// @param elapsed time elapse since current sdk instance join the channel in ms
252248 func rtcEngine( _ engine: AgoraRtcEngineKit , didJoinedOfUid uid: UInt , elapsed: Int ) {
249+ if uid == CAMERA_UID || uid == PLAYER_UID {
250+ return
251+ }
253252 LogUtils . log ( message: " remote user join: \( uid) \( elapsed) ms " , level: . info)
254253
255254 // Only one remote video view is available for this
@@ -294,7 +293,7 @@ extension MediaPlayerMain: AgoraRtcMediaPlayerDelegate {
294293 case . openCompleted:
295294 let duration = weakself. mediaPlayerKit. getDuration ( )
296295 weakself. playerControlStack. isHidden = false
297- weakself. playerDurationLabel. text = " \( String ( format: " %02d " , duration / 60 ) ) : \( String ( format: " %02d " , duration % 60 ) ) "
296+ weakself. playerDurationLabel. text = " \( String ( format: " %02d " , duration / 60000 ) ) : \( String ( format: " %02d " , duration % 60000 / 1000 ) ) "
298297 weakself. playerProgressSlider. setValue ( 0 , animated: true )
299298 break
300299 case . stopped:
@@ -308,12 +307,12 @@ extension MediaPlayerMain: AgoraRtcMediaPlayerDelegate {
308307 }
309308
310309 func agoraRtcMediaPlayer( _ playerKit: AgoraRtcMediaPlayerProtocol , didChangedToPosition position: Int ) {
311- let duration = Float ( mediaPlayerKit. getDuration ( ) * 1000 )
310+ let duration = Float ( mediaPlayerKit. getDuration ( ) )
312311 var progress : Float = 0
313312 var left : Int = 0
314313 if duration > 0 {
315- progress = Float ( mediaPlayerKit . getPosition ( ) ) / duration
316- left = Int ( ( mediaPlayerKit. getDuration ( ) * 1000 - mediaPlayerKit . getPosition ( ) ) / 1000 )
314+ progress = Float ( position ) / duration
315+ left = Int ( ( mediaPlayerKit. getDuration ( ) - position ) ) / 1000
317316 }
318317 DispatchQueue . main. async { [ weak self] in
319318 guard let weakself = self else { return }
@@ -324,5 +323,3 @@ extension MediaPlayerMain: AgoraRtcMediaPlayerDelegate {
324323 }
325324 }
326325}
327-
328- //#endif
0 commit comments