Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Nozioni di base
Ti consigliamo di integrare l'SDK del lettore tramite. CocoaPods (In alternativa, si può aggiungere il framework al progetto manualmente.)
Consigliato: integra il Player SDK () CocoaPods
Le versioni vengono pubblicate tramite CocoaPods under the nameAmazonIVSPlayer
. Aggiungere questa dipendenza al proprio Podfile:
pod 'AmazonIVSPlayer'
Eseguire pod install
e l'SDK sarà disponibile nel .xcworkspace
.
Approccio alternativo: installare manualmente il framework
-
Scarica l'ultima versione da https://player.live-video.net/1.24.0/AmazonIVSPlayer.xcframework.zip.
-
Estrai i contenuti dell'archivio. AmazonIVSPlayer.xcframework
contiene l'SDK sia per il dispositivo sia per il simulatore.
-
Incorporare AmazonIVSPlayer.xcframework
trascinandolo nella sezione Framework, librerie e contenuto incorporato della scheda Generali per il target dell'applicazione:
Creare lettore
L'oggetto lettore è IVSPlayer
. Può essere inizializzato come illustrato di seguito:
- Swift
-
import AmazonIVSPlayer
let player = IVSPlayer()
- Objective-C
-
#import <AmazonIVSPlayer/AmazonIVSPlayer.h>
IVSPlayer *player = [[IVSPlayer alloc] init];
Configurazione di delegati
I callback delegati forniscono informazioni sullo stato di riproduzione, eventi ed errori. Tutti i callback vengono richiamati nella coda principale.
- Swift
-
// Self must conform to IVSPlayer.Delegate
player.delegate = self
- Objective-C
-
// Self must conform to IVSPlayer.Delegate
player.delegate = self
Visualizzare video
Il lettore visualizza il video in un livello personalizzato, IVSPlayerLayer
. L'SDK fornisce inoltre IVSPlayerView
, una sottoclasse UIView
supportata da questo livello. È possibile usare il più conveniente per l'interfaccia utente dell'applicazione.
In entrambi i casi, visualizzare il video da un'istanza del lettore utilizzando la proprietà 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;
Caricare un flusso
Il lettore carica il flusso in modo asincrono. Il suo stato indica quando è pronto per la riproduzione.
- Swift
-
player.load(url)
- Objective-C
-
[player load:url];
Riprodurre un flusso
Quando il lettore è pronto, usare play
per iniziare la riproduzione. Utilizzare l'interfaccia delegato o l'osservazione di chiavi-valori sulla proprietà state
per osservare il cambiamento di stato. Di seguito viene riportato un esempio dell'approccio basato su delegati:
- 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];
}
}
Mettere in pausa quando l'app è in background
Il lettore non supporta la riproduzione mentre l'app è in background, ma non è necessario che venga chiuso del tutto. È sufficiente mettere in pausa; vedere gli esempi di seguito.
- 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];
}
Sicurezza del thread
L'API del lettore non è thread-safe. È necessario creare e utilizzare un'istanza del lettore dal thread principale dell'applicazione.
Mettere tutto insieme
Il seguente frammento semplice di controller di visualizzazione carica e riproduce un URL in una visualizzazione del lettore. Tenere presente che la proprietà playerView
viene inizializzata da un XIB/Storyboard e che la sua classe è impostata su IVSPlayerView
nell'Interface Builder utilizzando la sezione Classe personalizzata dell'Inspector dell'identità.
- 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