2016-11-01 9 views
1

私はAVPlayerViewControllerを使って、ストリーミングオーディオを再生するtvOS上のアプリを持っています。これは、バックグラウンドに行ってもうまく動作します。問題は、AppleTVがスリープ状態になり、スリープ状態になったときにプレイヤーが立ち往生し、アプリケーションの強制終了だけが私のプレイを再開できることです。睡眠からの目覚めと再生の再開

正常に再生を再開するためにアプリケーションで処理する必要があるイベントはありますか?ドキュメントでは何を探していますか?

不可欠なコードは次のとおりです。

override func viewDidLoad() { 
    super.viewDidLoad() 

    // loading details fron plist 
    if let stationInfo = readStationSettings() { 
     stationUrl = stationInfo["url"] as? String 
     stationName = stationInfo["name"] as? String 
     stationDescription = stationInfo["description"] as? String 
     stationImage = stationInfo["image"] as? UIImage 
    } else { 
     trackTitle = "Unable to find station details" 
     return 
    } 

    let videoURL = URL(string: stationUrl!) 

    do { 
     try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
     try AVAudioSession.sharedInstance().setActive(true, with: .notifyOthersOnDeactivation) 
    } catch let error as NSError { 
     print(error.localizedDescription) 
    } 

    playerItem = AVPlayerItem(url: videoURL!) 

    self.player=AVPlayer(playerItem: playerItem!) 

    playerLayer=AVPlayerLayer(player: player) 

    self.player?.play() 
} 
+0

はい、いくつかのコードを追加してください。 –

+0

@RomanPodymov質問を更新しました –

答えて

0

私はiOSのiPhoneアプリと同じ問題を持っていたし、私は解決策はあなたを助けるかもしれないと思います。私はAVPlayerViewControllerを使用して、プッシュされたビューコントローラでビデオを表示しています。ビデオがフルスクリーンで再生され、アプリケーションがスリープ状態になると、アプリケーションを終了しない限り、ビデオは再生を再開できません。これは実際にあなたと同じ問題です。

残念ながら、ソリューションにはAVPlayerViewControllerでプライベート関数を呼び出す必要があります。他の方法でフルスクリーンモードを終了する方法はありません。私AppDelegateは、プレーヤーのビューコントローラのインスタンスを保持し、アプリケーションがアクティブモードを辞任する場合、以下のコードが呼び出されます。

let appDelegate = UIApplication.getAppDelegate() 
if let player = appDelegate.playerController.player { 
    if let cl = NSClassFromString("AVFullScreenViewController") { 
     for window in UIApplication.sharedApplication().windows { 
      if let pvc = window.rootViewController?.presentedViewController { 
       if pvc.isKindOfClass(cl) { 
        let dSel = NSSelectorFromString("exitFullScreenAnimated:completionHandler:") 
        if appDelegate.playerController.respondsToSelector(dSel) { 
         appDelegate.playerController.performSelector(dSel, withObject: false, withObject: nil) 
        } 
        break 
       } 
      } 
     } 
    } 

    player.pause() 
    appDelegate.playerController.player = nil 
    appDelegate.playerController.removeFromParentViewController() 
    appDelegate.playerController.navigationController?.popViewControllerAnimated(false) 
} 

あなたが他のスウィフトかにそれを採用する必要がある場合がありますので、このコードは、スウィフト2.3で動作していますObjective Cのバージョン。 これは、リスクの高いビジネスであるプライベートAPI呼び出しを呼び出すことになります。 とにかく、これが助けてくれることを願っています。

も参照this thread Appleデベロッパフォーラムに