在后台播放 YouTubePlayer 声音 Swift

Playing YouTubePlayer Sound in Background Swift

我正在使用 YouTubeSDK 在我的应用程序中播放 YouTube 视频:https://github.com/gilesvangruisen/Swift-YouTube-Player

但是,当用户按下主页时,我希望声音在后台继续播放。 https://github.com/youtube/youtube-ios-player-helper/issues/38

我目前正在 didFinishLaunchingWithOptions 中的 App Delegate(糟糕的转换)中尝试此操作:

let sessionEror:NSError?
do {

    let playBG = try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .DefaultToSpeaker)

} catch let error as NSError  {
    print("ERROR")
}

这是在同一文件中它自己的函数:

func playerView(YTPlayerView: YTPlayerState, didChangeToState: YTPlayerState) {

        switch {

        case YTPlayerState.Paused:
            YTPlayerState.Playing,
        break
        }


    }

知道如何使用 YouTube SDK 在后台继续播放声音吗?另外,是否可以只在后台播放声音而不是视频?

感谢您提供的所有帮助。

分离 Youtube 视频的视频和音频是违反 YouTube Terms of Service 的。查看禁令

  1. separate, isolate, or modify the audio or video components of any YouTube audiovisual content made available through the YouTube API;

  2. promote separately the audio or video components of any YouTube audiovisual content made available through the YouTube API;

话虽这么说,解决方法仍然与解释的相同 here or here 这两者都表明您需要有一个委托视频播放的服务。直接,Youtube API 不允许你这样做。