When you're watching a video with MPMoviePlayerController and the user presses the top button to lock the screen, the app goes to sleep and so does the sound from the video.
Is there any way to prevent the lock from stopping the sound? If not, is there a way to intercept the lock, to create a "custom lock", to save some battery but keep playing the video?
Sounds like you haven't set your audio session category. Set the AVAudioSession's category property to AVAudioSessionCategoryPlayback to indicate that the app's major purpose is to play back audio, and it therefore should ignore the screen lock button and ring/silent switch. If you're working with the lower-level C API, you'll be using AudioSessionSetProperty(kAudioSessionCategory_MediaPlayback), but it's the same concept.
Related
I am using MPMoviePlayerController to play streaming audio. I'm trying to get background audio working correctly. Right now, audio continues to play when you exit the app - the lock-screen and multi-tasking bar controls even work.
When a song finishes, the app is supposed to advance to the next track and play it. It works when the app is open but not when it is in the background (a song finishes but does not advance to the next track). If a song finishes and you re-open the app, however, the next song will start up immediately.
I am currently using NSNotificationCenter to keep track of when tracks end to advance to the next track (in my app delegate). Again, it works like a charm when the app is open. Is there a better way to do this to keep audio playing after a song is done?
I had this issue lately. Hope the answer helps other people.
If you have a playlist for example and want to play the next song while in background mode or lock mode add this line of code on your viewDidLoad:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
This makes the app supports remote control events.
I have an application which uses Audio Queues to do recording of spoken user input.
This only happens for short durations and only when the user presses a button.
I'd like to allow the user's background music to continue playing, except have it muted or playing at a much lower volume whenever the recording is actually taking place (to avoid recording the audio playing out of the speaker).
For my recording queue, I'm using the PlayAndRecord category, and I have the OverrideCategoryMixWithOthers property set to true.
This problem became less of an issue in iOS4+, when they added the AudioSessionSetActiveWithFlags API and the kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation flag.
To pause background music while recording, you need to call AudioSessionSetActive(false), which will deactivate your audio session, then switch categories to PlayAndRecord mode, and set the OverrideCategoryMixWithOthers property to false.
Now, reactivate your session with AudioSessionSetActive(true) with these settings, the audio session for iPod/Pandora/etc will be interrupted and will fade-out and pause.
Then, when your recording is finished, deactivate your session again using
AudioSessionSetActiveWithFlags(false, kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation);
This will notify the background music apps that their session is no longer interrupted and they can begin playing music.
Finally, you can set up your audio session again using MediaPlayback (or equiv.) mode and setting the OverrideCategoryMixWithOthers property to true again.
I'm using MPMoviePlayerViewController and I want to know if there is a method that can tell me when the user clicks on the Pause button for the video, like an observer?
You should use MPMoviePlayerController instead :
Movie Player Notifications
The MPMoviePlayerController class generates numerous notifications to keep your application informed about the state of movie playback. In addition to being notified when playback finishes, interested clients can be notified in the following situations:
When the movie player begins playing, is paused, or begins seeking forward or backward
When the scaling mode of the movie changes
When the movie enters or exits fullscreen mode
When the load state for network-based movies changes
When meta information about the movie itself becomes available
For more information, see the Notifications section in this document.
http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html
I'm using AVAudioPlayer to play sounds on a game. When the user locks the device, I pause the game (and send it to a resume view) and want all the sounds to stop playing.
However, since the resume view is the same I use when loading a saved game, that view triggers a sound using an AVAudioPlayer. When it does so with the screen locked, the sound is audible, even though I'm setting the audio session to kAudioSessionCategory_SoloAmbientSound.
Does anyone know how to stop all sounds even if AVAudioPlayer play is called?
I tried calling this method:
AudioSessionSetActive(false)
but it didn't help.
Update: I'm currently setting the volume to 0.0 when the device is locked, however, I wanted to know if there was a built-in way via AudioSessions, etc.
Thank you
The best way I could find is to set the volume to 0 on lock events, and restore it when the device gets unlocked.
So I have an app which plays many short sound clips. I need to know when the sounds are finished playing, and I need to use mp3s, so I'm using AVFoundation for the sound playback.
When a sound is actively playing, and the user uses the hardware volume buttons, the playback volume changes. Problem is, the app is NOT constantly playing sounds, and when it's not, and the hardware buttons are used, the RINGER volume gets adjusted instead.
How do I set it up so, as long as the app is running, the user can adjust the playback volume?
Thanks!
Turns out this can be accomplished by allocating an AVAudioPlayer with any valid sound file and calling the prepareToPlay method, without ever calling the play method.
Works perfectly.
Start an AudioSession and don't stop it when you're not playing sounds.
so you want to disable ringer playback volume as long as the app is running? therefore the hardware controls will only adjust the app playback sounds?
i dont think this is possible unless you are "always playing sound" for example, many games are always playing background music or what have you.
You might be able to accomplish this by constantly playing a 0 volume sound as long as your app is running. You could then play your sound clips over it.
How do I play multiple sounds simultaneously?