How to mute background music in iOS - ios

I have added music to my app so the music plays as soon as the app starts through the app delegate.
Now I have a switch in my app that I would like to rig up to where when it is switched off, the music stops playing, and when its switched back on, it resumes playing. Only problem is I don't know how to do this.
I know that I would need to make an IBAction and rig it to my switch but I am new to iOS Development and I learn best by being told how to do something and then applying it and seeing how it works in real time.
It would be great if someone could provide the code so I can throw it into my project and see how it works and learn from this.
Thanks.

When you pressing Mute button first check audioplayer is playing or not for exa.
if([avAudio isPlaying])
{
[avAudio pause];//OR[avAudio stop];
}
else
{
[avAudio play];
}

Related

iOS: AVPlayer play won't play sometimes

So I have an app built with a player that plays a video, I have a [player pause] and [player play] in the didBecomeActive and willResignActive methods. Most of the time works fine, but when I open the app, and press the home button and repeat again that process, around the 8th time the video will not play even though I see the play method getting called.
Does anyone have any idea what might be going on?
The app can be in several states that are not foreground. Before playing, check to see if you still have a player, that it still has a player.currentItem, and if it's status is AVPlayerStatusReadyToPlay.
If any of those conditions are not met, then the player and the item must be reinitialized using the code that you used to create it in the first place.
This is a good candidate for a lazy initializer for your player property.

How can I avoid the AVPlayerLayer `setPlayer` audio blip?

I have an application that plays video using AVPlayer and AVPlayerLayer. In my app, I'm able to play audio when the app is locked by setting the player property of my AVPlayerLayer to nil when the application enters the background:
-(void)appEnteredBackgroundNotification:(NSNotification *)notification {
[[self playerLayer] setPlayer:nil];
}
However, when I do this, the audio will lag/blip for around 0.5 seconds. This sounds really really bad for the end user. Same goes for when the app enters foreground and I re-set the player property.
How can I avoid this audio blip? As a test I've tried removing the player in a background thread to no avail.
Update: I spoke with an Apple engineer at WWDC and they said that this issue is a bug on their end (so far not fixed in iOS 9) and this approach is the correct approach. Great...
I think may not you call pause before setting to nil and vice versa. And, try calling prepare before play.

Resuming Spotify after pause on iOS

I have an app that I want to be able to pause and resume the current playing song. The only way I've found so far is this:
[[MPMusicPlayerController systemMusicPlayer] pause];
// some time passes
[[MPMusicPlayerController systemMusicPlayer] play];
The problem is that if it's a 3rd party app that is playing, like Spotify, it seems to lose focus to the iOS Music.app when I do this. So when I try to resume with [ play], the last played song in Music.app starts playing.
Is there a way to pause and resume Spotify (or whatever app was playing) without losing focus to the Music.app? Aka, the exact same behaviour as pressing play/pause in iOS Action Center
There's sadly no way to do this (with public API that is, there are private API's but if you want to go in the AppStore you can't use them).

Pause all audio from the background - iOS Tweak

I am making a jailbroken iOS tweak and have a question. How can I stop all device audio while running from the background. Example: The user is playing Spotify and then clicks the power button on the device(turning off the screen and putting it to sleep) meanwhile Spotify is still playing music. At this point how can I stop stop all audio on the device(whether it's Spotify, default music app, Pandora, or whatever)? I have look into AVAudioSession and AVAudioPlayer but have not found a consistent reliable method to do this. I am testing this on a jailbroken iPhone5 running iOS7.0.4.
I am open to using AVAudioSession or AVAudioPlayer if it works consistently. I am also open to using something from a private framework to accomplish this. Thanks!
Thanks!
I figured it out, use this:
SBMediaController *mainMediaController = [%c(SBMediaController) sharedInstance];
[mainMediaController pause];

Opening Siri stops AVAudioPlayer

I am currently using an AVAudioPlayer to play looped music in the background of my app. However, when siri is opened in my application it turns it off. I am farly experienced with objective c but I cant seem to get this to work. Is there any way I can tell when a user opens siri so that I can start the AVAudioPlayer again after it stops. Please help!
P.S I have attempted continuously playing the audio but then siri wont work.
You can use AVAudioPlayerDelegate's audioPlayerEndInterruption:withOptions:
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags {
[player play];
}
For anyone else coming to this, the endInterruption delegate isn't getting called from me when my app gets interrupted by Siri. I had to make the app resume playing when it becomes active again (using a notification).

Resources