5
votes

Im using YouTube Helper Library to play YouTube videos on my iOS application
Here are link of Library I just need a way to continue audio (or video) playing in background ,
So when user click on home button i need him to be able to hear the voice
of the video ,
even when he lock his device i need to him to be able to control the sound and click next and back button for audio playing .. How i can achieve that on YouTube Helper Library ?

NOTE: many application do that and they exist on app-store like iMusic , Video Tube ,MusicTV , MB2

3
the above lib uses webview to play video .i don't think there is a way to play video in background until you make a custom video player.link..rahul_send89

3 Answers

3
votes

Playing background video (or voice) is against the YouTube TOS. You can not do it with this library for sure, even if you figure out a workaround, your app would be taken down because of TOS violation.

1
votes

About playing sounds in background:

You have to set the AVAudioSession category to AVAudioSessionCategoryPlayback and add the audio value to the UIBackgroundModes key in your information property list file.

NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];

I have tested this code on iOS 7 and up only, so it may not work for earlier versions.

From Apple docs:

AVAudioSessionCategoryPlayback The category for playing recorded music or other sounds that are central to the successful use of your app.

When using this category, your app audio continues with the Silent switch set to silent or when the screen locks. (The switch is called the Ring/Silent switch on iPhone.) To continue playing audio when your app transitions to the background (for example, when the screen locks), add the audio value to the UIBackgroundModes key in your information property list file.

see references here and here

0
votes

You need to enable it when app enter background. Remember to check your app is in background

func playerView(_ playerView: YTPlayerView, didChangeTo state: YTPlayerState) {
    if state == .paused {
        playerView.playVideo()
    }
}