开始使用 - Amazon Interactive Video Service

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

开始使用

我们建议您通过集成播放器 SDK CocoaPods。(或者,您可以手动将框架添加至项目。)

推荐:集成播放器 SDK (CocoaPods)

发行版以该 CocoaPods 名称发布AmazonIVSPlayer。将此依赖项添加至您的 Podfile 中:

pod 'AmazonIVSPlayer'

运行 pod install,开发工具包将在 .xcworkspace 中可用。

替代方法:手动安装框架

  1. https://player.live-video.net/1.24.0/AmazonIVSPlayer.xcframework.zip 下载最新版本。

  2. 提取归档的内容。AmazonIVSPlayer.xcframework 包含适用于设备和模拟器的开发工具包。

  3. 通过以下方法嵌入 AmazonIVSPlayer.xcframework:将其拖动到应用程序目标 General(常规)选项卡上的 Frameworks, Libraries, and Embedded Content(框架、库和嵌入式内容)部分:

    应用程序目标 General(常规)选项卡上的 Frameworks, Libraries, and Embedded Content(框架、库和嵌入式内容)部分:

创建播放器

播放器对象是 IVSPlayer。按如下方法进行初始化:

Swift
import AmazonIVSPlayer let player = IVSPlayer()
Objective-C
#import <AmazonIVSPlayer/AmazonIVSPlayer.h> IVSPlayer *player = [[IVSPlayer alloc] init];

设置委托

委托回调提供有关回放状态、事件和错误的信息。所有回调都在主队列上调用。

Swift
// Self must conform to IVSPlayer.Delegate player.delegate = self
Objective-C
// Self must conform to IVSPlayer.Delegate player.delegate = self

显示视频

播放器在自定义层 IVSPlayerLayer 中显示视频。软件开发工具包还提供了 IVSPlayerView,自定义层支持的 UIView 子类。选择更方便您的应用程序界面的一种。

在这两种情况下,使用 player 属性,均可显示播放器实例中的视频。

Swift
// When using IVSPlayerView: playerView.player = player // When using IVSPlayerLayer: playerLayer.player = player
Objective-C
// When using IVSPlayerView: playerView.player = player; // When using IVSPlayerLayer: playerLayer.player = player;

加载流

播放器以异步方式加载流。其状态指示何时可以播放。

Swift
player.load(url)
Objective-C
[player load:url];

播放视频流

播放器准备就绪后,使用 play 按钮开始播放。使用 state 属性中的委托界面或键值观察来观察状态变化。以下是基于委托方法的示例:

Swift
func player(_ player: IVSPlayer, didChangeState state: IVSPlayer.State) { if state == .ready { player.play() } }
Objective-C
- (void)player:(IVSPlayer *)player didChangeState:(IVSPlayerState)state { if (state == IVSPlayerStateReady) { [player play]; } }

暂停后台应用程序

播放器不支持后台播放,但不需要完全退出。暂停即可;请参阅下面的示例。

Swift
override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: UIApplication.didEnterBackgroundNotification, object: nil) } @objc func applicationDidEnterBackground(_ notification: NSNotification) { playerView?.player?.pause() }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; NSNotificationCenter *defaultCenter = NSNotificationCenter.defaultCenter; [defaultCenter addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil]; } - (void)applicationDidEnterBackground:(NSNotification *)notification { [playerView.player pause]; }

线程安全

播放器 API 不是线程安全的。您应该从应用程序主线程创建和使用播放器实例。

组合起来

以下简单的视图控制器代码段在播放器视图中加载并播放 URL。请注意,playerView 属性是从 XIB/Storyboard 初始化而来,并且使用 Identity Inspector 中的“自定义类”部分在 Interface Builder 中将其类设置为 IVSPlayerView

Swift
import AmazonIVSPlayer class MyViewController: UIViewController { ... // Connected in Interface Builder @IBOutlet var playerView: IVSPlayerView! override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: UIApplication.didEnterBackgroundNotification, object: nil) } @objc func applicationDidEnterBackground(_ notification: NSNotification) { playerView?.player?.pause() } ... // Assumes this view controller is already loaded. // For example, this could be called by a button tap. func playVideo(url videoURL: URL) { let player = IVSPlayer() player.delegate = self playerView.player = player player.load(videoURL) } } extension MyViewController: IVSPlayer.Delegate { func player(_ player: IVSPlayer, didChangeState state: IVSPlayer.State) { if state == .ready { player.play() } } }
Objective-C
// MyViewController.h @class IVSPlayerView; @interface MyViewController: UIViewController ... // Connected in Interface Builder @property (nonatomic) IBOutlet IVSPlayerView *playerView; ... @end // MyViewController.m #import <AmazonIVSPlayer/AmazonIVSPlayer.h> @implementation MyViewController <IVSPlayerDelegate> ... - (void)viewDidLoad { [super viewDidLoad]; NSNotificationCenter *defaultCenter = NSNotificationCenter.defaultCenter; [defaultCenter addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil]; } - (void)applicationDidEnterBackground:(NSNotification *)notification { [playerView.player pause]; } // Assumes this view controller is already loaded. // For example, this could be called by a button tap. - (void)playVideoWithURL:(NSURL *)videoURL { IVSPlayer *player = [[IVSPlayer alloc] init]; player.delegate = self; playerView.player = player; [player load:videoURL]; } - (void)player:(IVSPlayer *)player didChangeState:(IVSPlayerState)state { if (state == IVSPlayerStateReady) { [player play]; } } ... @end