Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] conditional import third party player in audio route #392

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
import UIKit
import AGEVideoLayout
import AgoraRtcKit
import AVKit

#if canImport(IJKMediaFramework)
import IJKMediaFramework
#elseif canImport(MobileVLCKit)
import MobileVLCKit
#endif

enum ThirdPlayerType: String {
case ijk = "ijkplayer"
case vendor = "thirdPartyPlayer"
case origin = "avplayer"
}

Expand All @@ -24,26 +30,26 @@ class AuidoRouterPlayerEntry: UIViewController {
@IBOutlet var orientationBtn: UIButton!
@IBOutlet weak var chosePlayerButton: UIButton!
var width: Int = 960, height: Int = 540, orientation: AgoraVideoOutputOrientationMode = .adaptative, fps = 15
private var playerType: ThirdPlayerType = .ijk
private var playerType: ThirdPlayerType = .origin

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func onChosePlayerType(_ sender: UIButton) {
let style: UIAlertController.Style = UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet
let alert = UIAlertController(title: "Player Type(ijkplayer/avplayer)".localized,
let alert = UIAlertController(title: "Player Type(thirdPartyPlayer/avplayer)".localized,
message: nil,
preferredStyle: style)
alert.addAction(getPlayerAction(ThirdPlayerType.ijk.rawValue))
alert.addAction(getPlayerAction(ThirdPlayerType.vendor.rawValue))
alert.addAction(getPlayerAction(ThirdPlayerType.origin.rawValue))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
func getPlayerAction(_ title: String) -> UIAlertAction {
return UIAlertAction(title: title, style: .default, handler: { [unowned self] _ in
self.chosePlayerButton.setTitle(title, for: .normal)
self.playerType = ThirdPlayerType(rawValue: title) ?? .ijk
self.playerType = ThirdPlayerType(rawValue: title) ?? .vendor
})
}
func getResolutionAction(width: Int, height: Int) -> UIAlertAction {
Expand Down Expand Up @@ -128,7 +134,8 @@ class AuidoRouterPlayerMain: BaseViewController {
@IBOutlet weak var container: AGEVideoContainer!
var agoraKit: AgoraRtcEngineKit!
private let videoString = "https://agora-adc-artifacts.s3.cn-north-1.amazonaws.com.cn/resources/sample.mp4"
private lazy var ijkPlayer: IJKAVMoviePlayerController? = {
private lazy var vendorPlayer: Any? = {
#if canImport(IJKMediaFramework)
let player = IJKAVMoviePlayerController(contentURL: URL(string: videoString))
player?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
player?.view.frame = playerView.bounds
Expand All @@ -139,7 +146,20 @@ class AuidoRouterPlayerMain: BaseViewController {
player?.allowsMediaAirPlay = true
player?.isDanmakuMediaAirPlay = true
return player
#elseif canImport(MobileVLCKit)
let player = VLCMediaPlayer()
if let url = URL(string: videoString) {
let media = VLCMedia(url: url)
player.media = media
}
let videoView = UIView(frame: playerView.bounds)
player.drawable = videoView
player.play()
return player
#endif
return nil
}()

private lazy var avPlayer: AVPlayerViewController? = {
guard let url = URL(string: videoString) else { return nil }
let player = AVPlayer(url: url)
Expand Down Expand Up @@ -232,16 +252,21 @@ class AuidoRouterPlayerMain: BaseViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let playerType = ThirdPlayerType(rawValue: configs["playerType"] as? String ?? "")
if playerType == .ijk {
setupIJKPlayer()
if playerType == .origin {
setupVendorPlayer()
} else {
setupAVPlayer()
}
}

private func setupIJKPlayer() {
guard let ijkPlayerView = ijkPlayer?.view else { return }
private func setupVendorPlayer() {
#if canImport(IJKMediaFramework)
guard let ijkPlayerView = (vendorPlayer as? IJKAVMoviePlayerController)?.view else { return }
playerView.addSubview(ijkPlayerView)
#elseif canImport(MobileVLCKit)
guard let vlcPlayerView = (vendorPlayer as? VLCMediaPlayer)?.drawable as? UIView else { return }
playerView.addSubview(vlcPlayerView)
#endif
}

private func setupAVPlayer() {
Expand All @@ -268,7 +293,11 @@ class AuidoRouterPlayerMain: BaseViewController {
if playerType == .origin {
avPlayer?.player?.pause()
} else {
ijkPlayer?.shutdown()
#if canImport(IJKMediaFramework)
(vendorPlayer as? IJKAVMoviePlayerController)?.shutdown()
#elseif canImport(MobileVLCKit)
(vendorPlayer as? VLCMediaPlayer)?.stop()
#endif
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22155" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21678"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22131"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -33,9 +33,9 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nd3-pG-lkL">
<rect key="frame" x="33" y="106" width="308" height="35"/>
<rect key="frame" x="24.5" y="106" width="325" height="41"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" title="Chose Player Type(ijkplayer/avplayer)"/>
<buttonConfiguration key="configuration" style="plain" title="Chose Player Type(ThirdParty/avplayer)"/>
<connections>
<action selector="onChosePlayerType:" destination="O0d-ef-mTa" eventType="touchUpInside" id="9vB-or-yXE"/>
</connections>
Expand Down
4 changes: 3 additions & 1 deletion iOS/APIExample/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ target 'APIExample' do
pod 'Floaty', '~> 4.2.0'
pod 'AGEVideoLayout', '~> 1.0.2'
pod 'CocoaAsyncSocket', '7.6.5'
pod 'ijkplayer', '~> 1.1.3'
# 如需测试SDK与三方播放器的”AudioRouter“兼容,可以使用ijkplayer或MobileVLCKit
# pod 'ijkplayer', '~> 1.1.3'
# pod 'MobileVLCKit', '3.5.1'
pod 'SwiftLint', '~> 0.53.0'
pod 'AgoraRtcEngine_iOS', '4.3.1'
# pod 'sdk', :path => 'sdk.podspec'
Expand Down
Loading