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
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.
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
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()
}
}
Related
Referring to the document https://developer.apple.com/library/ios/qa/qa1668/_index.html ,
I have modified the info.plist to support playback for HLS stream while app is in background, and removed/restored AVPlayer when app goes to background/comes to foreground using Application delegate events(application: app didBecomeActive and application: app didEnterBackground) , am certain that code gets executed as I can see the logs. Yet when I navigate out of the app by pressing home key on actual device, Airplay stops. Also, I added a KVObserver on the rate property of AVPlayer, the rate is 1 while the app is minimized.
One thing that I noticed was for the same piece of code, on simulator, If I simulate home key press (cmd+shift+h) , the audio for the video is audible while I am outside the app, while it stops on actual device. Am I doing something wrong?
Had to put the methods :
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
in the app delegate, thought they were just for audio streaming, but for video as well are required for multitasking, this resolved the issue.
I'm building an app where the user can shoot a video.
We've noticed that after presenting the camera and dismissing it, the mute switch is no longer respected and the sound from the video is audible! The video is played with an AVPlayer.
Is this a bug for Apple?
It is not a bug. AVPlayer lets you categorize your audio playback to tell iOS your intent. Here's how to set it:
[[AVAudioSession sharedInstance] setCategory:cat error:&err];
where cat is one of
AVAudioSessionCategoryAmbient: String
AVAudioSessionCategorySoloAmbient: String
AVAudioSessionCategoryPlayback: String
AVAudioSessionCategoryRecord: String
AVAudioSessionCategoryPlayAndRecord: String
AVAudioSessionCategoryAudioProcessing: String
AVAudioSessionCategoryMultiRoute: String
For example, the documentation for AVAudioSessionCategoryPlayback says
When using this category, your app audio continues with the Silent switch set to silent or when the screen locks.
Setting it to AVAudioSessionCategorySoloAmbient should make it respect the Mute button.
I have an app that needs to use AVAudioSessionCategoryPlayback in order to play sound while the device is locked or my app is in the background. (It's an alarm feature.) So I also need to set the "audio" key in my UIBackgroundModes list.
I have put the following code in my application:didFinishLaunchingWithOptions: method:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
However, I don't want to interrupt any music that is already playing on the device. When my app is launched, it causes Spotify (for example) to stop playing music. I can switch to Spotify and press play, and when I switch back to my app, it continues playing.
But this is very annoying to the user - alarms are not the only thing my app does, and there are many reasons for them to launch it that don't involve setting alarms. I don't think the user would ever want my app to stop any other audio playing on the device.
Ideally I'd never interrupt already-playing audio. I could also live with stopping audio when an alarm is set (at which point I have to activate my audio session to ensure that I'm able to play it) but it seems like there is no way for me to avoid the audio stop at app launch.
Is there some way to start my app with the audio session disabled, or to start it with this option already applied?
It turns out that I was calling .prepareForPlayback() on an AVAudioPlayer in an init() method which was getting called long before my didFinishLaunchingWithOptions: fired.
I have done several apps with buzztouch for Iphone and Ipads, here is a free one as an example :
http://itunes.apple.com/us/app/lr-basics-free-edition/id497563707?mt=8
I do not know code very much, I have very little basics that is why I designed my app using Buzztouch, which by the way is amazing !
The only one problem that I have is that the default behavior when you play a video (which was most of my apps are doing, playing tutorials) and if the silent mode is on either on Ipad or Iphone, there is no audio, even thought the volume slider is active, given the impressions to users that there is a bug, and I got some bad reviews due to that, I have also people writing me about it. I Then tell them all they have to do is to turn off the silent mode and they the audio is back, works everytime, but in the meantime I get complaits !
So here is my questions, is there a simple way I can located in the Buzztouch generated code a property that can easely be changed so that when a video is played, audio stays, despiste the silent mode being active.
I actually checked 4 or 5 other Iphones similar applications including the default youtube apple app, the default is that the audio plays even if the silent mode is turn on, giving my customer the impression that my app is bugged.
I'm not a programmer so please but as simple as possible in your answer.
Tku so much for the help.
Serge
What you are describing is the default behavior in iOS - when the ring/silent switch is in silent mode all audio from your app will be suppressed.
I don't know about implementing this from BuzzTouch, but here is a native solution I used to get around this for one of my apps that plays video:
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:myVidURL];
... set up player ...
// prevent mute switch from switching off audio from movie player
NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];
[self presentMoviePlayerViewControllerAnimated:mpvc];
You will also need to include the AVFoundation framework for this to work.
Here's the link where I first found this tip:
http://www.24100.net/2011/05/ignore-ringtone-mute-switch-during-mpmovieplayer-video-playback-ios/
Right from documentation - iOS has 6 audio session categories out of that 3 affects the behavior of Slient switch:
AVAudioSessionCategoryAmbient or the equivalent kAudioSessionCategory_AmbientSound— Using this category, your audio is silenced by the Ring/Silent switch and when the screen locks. Used when we want our app audio with built-in app audio
AVAudioSessionCategorySoloAmbient or the equivalent kAudioSessionCategory_SoloAmbientSound—Use this category for an application whose audio you want silenced when the user switches the Ring/Silent switch to the “silent” position and when the screen locks. This is the default category
AVAudioSessionCategoryPlayback or the equivalent kAudioSessionCategory_MediaPlayback—Use this category for an application whose audio playback is of primary importance. Your audio plays even with the screen locked and with the Ring/Silent switch set to silent.
Hint: Upload the audio file to your buzztouch account files. This is the only way Buzztouch will recognize the audio. next, add the audio file to your xcode bttouch sound folder and add all references. You should be fine!
We built our music-oriented app in html5 and javascript with Sencha touch. For distribution we wrapped it in xcode with UIwebView. Everything runs fine except one thing that does not work: audio playing in multitask mode.
I know the general idea: add the UIBackgroundModes in info.plist.
Done. Now we can play the audio even in background mode.
Until we reach the end of the song. To start the next song we have to bring the app to foreground again or we can hit the play or 'next song' button on the iPhone audio controller.
After some research I found a promising workaround at: " Entering background on iOS4 to play audio " where the workaround is to edit AVAudioSessionCategoryPlayback and work with the UIBackgroundTaskIdentifier.
The problem for me is (just like in any other fix I found so far) that those solutions always assume that the audio is played either with AVaudioPlayer or MPMusicPlayerController. But in my case I user neither, our audio is played by our html5 player wrapped in UIwebView.
Anyone has any advice on how to continue playing the audio in iOS multitask mode when the audio player is a html5/javascript player?
My app plays audio via an <audio> tag in html hosted in UIWebView, and it supports background playing.
As you suggest, you need to have the 'App plays audio' background mode defined in your plist.
Try adding this to your applicationDidFinishLaunching: handler:
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
Thanks, guys, but I ended up simply keeping the app busy in a loop when in background mode. This was enough to bridge the time when connecting to the next song on the playlist.
The code I used is similar to this one:
iPhone - Backgrounding to poll for events
You could try this, adding a function on the onend event.