Playing YouTubePlayer Sound in Background Swift

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 10:40:52

问题


I'm using the YouTubeSDK to play YouTube videos in my app found here: https://github.com/gilesvangruisen/Swift-YouTube-Player

However, when the user presses home I want the sound to keep playing in the background. https://github.com/youtube/youtube-ios-player-helper/issues/38

I'm currently trying this in the App Delegate (awful conversion) in the didFinishLaunchingWithOptions:

let sessionEror:NSError?
do {

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

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

And this is in it's own function in the same file:

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

        switch {

        case YTPlayerState.Paused:
            YTPlayerState.Playing,
        break
        }


    }

Any idea how to keep playing the sound in the background using the YouTube SDK? Also, is it possible to just play the sound in the background and not the video?

Any and all help is appreciated.


回答1:


Separating out the video and audio of a Youtube video is against YouTube Terms of Service. Look under Prohibitions

  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;

That being said, the workaround still remains the same as explained here or here which both suggest that you need to have a service that delegates the video's playback. Directly, the Youtube API will not allow you to do so.



来源:https://stackoverflow.com/questions/34029098/playing-youtubeplayer-sound-in-background-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!