Play YouTube videos in the background using the YouTube iOS Assistant Library

I use the YouTube Helper Library to play YouTube videos in the iOS app
Here is the library link I just need a way to continue playing audio (or video) in the background,
Therefore, when the user clicks the home button, I need him to be able to hear the voice of the video,
even when it locks its device, I need it to be able to control the sound and click the "Next" and "Back" buttons to play the sound. How can I achieve this in the YouTube Helper Library?

NOTE. Many applications do this, and they exist in the application store such as iMusic, Video Tube, MusicTV, MB2

+5
source share
3 answers

About playing sounds in the background:

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

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

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

From Apple docs:

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

When using this category, your audio application continues using the silent switch off or when the screen locks. (The Ring / Silent switch on the iPhone is called up.) To continue playing the sound when your application goes to the background (for example, when the lock screen), add the sound value to the UIBackgroundModes key in your information properties list file.

see links here and here

+1
source

Play background video (or voice) against YouTube TOS . You cannot do this with this library for sure, even if you figure out a workaround, your application will be terminated due to a TOS violation.

+3
source

You need to enable it when the application enters in the background. Remember to check that your application is in the background

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

Source: https://habr.com/ru/post/1204238/


All Articles