Skip to content

Commit 0098a3e

Browse files
author
xianing
committed
fix apiExample remaining issues
1 parent d86ef26 commit 0098a3e

9 files changed

Lines changed: 71 additions & 84 deletions

File tree

iOS/APIExample/Examples/Advanced/MediaChannelRelay/MediaChannelRelay.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ class MediaChannelRelayMain: BaseViewController {
102102
// 2. If app certificate is turned on at dashboard, token is needed
103103
// when joining channel. The channel name and uid used to calculate
104104
// the token has to match the ones used for channel join
105-
let result = agoraKit.joinChannel(byToken: nil, channelId: channelName, info: nil, uid: 0) {[unowned self] (channel, uid, elapsed) -> Void in
106-
self.isJoined = true
107-
LogUtils.log(message: "Join \(channel) with uid \(uid) elapsed \(elapsed)ms", level: .info)
108-
}
105+
let option = AgoraRtcChannelMediaOptions()
106+
option.publishCameraTrack = .of(true)
107+
option.clientRoleType = .of((Int32)(AgoraClientRole.broadcaster.rawValue))
108+
109+
let result = agoraKit.joinChannel(byToken: KeyCenter.Token, channelId: channelName, uid: 0, mediaOptions: option)
109110
if result != 0 {
110111
// Usually happens with invalid parameters
111112
// Error code description can be found at:
@@ -143,8 +144,6 @@ class MediaChannelRelayMain: BaseViewController {
143144
override func willMove(toParent parent: UIViewController?) {
144145
if parent == nil {
145146
// leave channel when exiting the view
146-
// deregister packet processing
147-
AgoraCustomEncryption.deregisterPacketProcessing(agoraKit)
148147
if isJoined {
149148
agoraKit.leaveChannel { (stats) -> Void in
150149
LogUtils.log(message: "left channel, duration: \(stats.duration)", level: .info)
@@ -238,4 +237,9 @@ extension MediaChannelRelayMain: AgoraRtcEngineDelegate {
238237
func rtcEngine(_ engine: AgoraRtcEngineKit, didReceive event: AgoraChannelMediaRelayEvent) {
239238
LogUtils.log(message: "didReceiveRelayEvent: \(event.rawValue)", level: .info)
240239
}
240+
241+
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinChannel channel: String, withUid uid: UInt, elapsed: Int) {
242+
self.isJoined = true
243+
LogUtils.log(message: "Join \(channel) with uid \(uid) elapsed \(elapsed)ms", level: .info)
244+
}
241245
}

iOS/APIExample/Examples/Advanced/MediaPlayer/Base.lproj/MediaPlayer.storyboard

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
33
<device id="retina6_1" orientation="portrait" appearance="light"/>
44
<dependencies>
55
<deployment identifier="iOS"/>
6-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
77
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
88
<capability name="System colors in document resources" minToolsVersion="11.0"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@@ -91,7 +91,7 @@
9191
<action selector="doOpenMediaUrlWithSender:" destination="jxp-ZN-2yG" eventType="touchUpInside" id="vVg-Wv-87g"/>
9292
</connections>
9393
</button>
94-
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="1" continuous="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qN0-s6-ZGP">
94+
<slider opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="1" continuous="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qN0-s6-ZGP">
9595
<rect key="frame" x="18" y="564" width="334.5" height="31"/>
9696
<connections>
9797
<action selector="doSeekWithSender:" destination="jxp-ZN-2yG" eventType="valueChanged" id="pnh-Up-W0k"/>

iOS/APIExample/Examples/Advanced/MediaPlayer/MediaPlayer.swift

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import UIKit
99
import AGEVideoLayout
1010
import 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

1515
class 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

Comments
 (0)