I'm programming a little SpriteKit game and I want to add a mute button, so that you can hear your own music while playing my game. Currently I'm just stopping the audio playback for the scene the mute button is in (main menu). However my game still automatically stops the user music even if the audio playback is stopped. How can I prevent that?
Also, I'd like to pass the information that the mute button is pressed (located in the main menu) to my game scene, so that I can also mute the music there, how do I do that?
Basically how do I pass a variable value from one scene to another?
What I did a while ago is check if the iPod Player is working and give it priority over the game music. (ie. Check if iPod is playing to actually start game music, as ipod music player will usually overwrite you game's music if you are using AVAudioPlayer.
+ (bool) IsIpodPlaying{
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying
|| [[MPMusicPlayerController applicationMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying){
NSLog(#"iPod IS playing");
return true;
}else{
NSLog(#"iPod NOT playing");
return false;
}
}
Obviously you can do the same for your mute button. Ignore any action if iPod is playing, otherwise stop your game music.
To pass the information when the Mute button is pressed you can (in order of personal preference):
Set your Menu scene as a delegate of your Game scene and create a method to enable or disable sounds (ie. userDidEnableSound: )
Create a SoundControl singleton class that has the enable and disable sound methods and access them wherever you want.
Send a notification using the NSNotificationCenter and wait for it in the menu.
Related
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).
I currently have two scenes in my game
When entering scene one music plays. When the game is over and the user enters scene two, the music stops. When opting to retry the game, the music plays again.
I attempted to code the music to play continuously
[self runAction:[SKAction playSoundFileNamed:#"YourFileName.extension" waitForCompletion:NO]];
This works fantastically in the sense that when the game is over, the music is still playing rather than being cut off. However, when opting to retry and entering scene one again, the music continues but is also triggered again, resulting in a duplication. This can keep happening.
Is there a way to detect if the audio is already playing so that it won't continue to get triggered every time I enter scene one?
Create a bool initialized to NO:
bool isMusicPlaying;
When you start the music, set the bool to YES and make sure music does not get started if already playing :
if(isMusicPlaying == NO)
{
[self runAction:[SKAction playSoundFileNamed:#"YourFileName.extension" waitForCompletion:NO]];
isMusicPlaying = YES;
}
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];
}
In the multimedia controls (in the multitasking UI) the "play" button always show the pause image even when the music is paused, everything works fine except this
this problem don't happen if the background audio comes from the Music App (i.e. the button image switch from play to pause and vice-versa as expected)
how to fix?
thanks
In my case, I had to pause all the AV-related items which were "playing":
[myAVAudioPlayer pause];
[myAVAudioRecorder pause];
[[MPMusicPlayerController applicationMusicPlayer] pause];
After which, the play/pause button on the lock screen switches, and the playhead also freezes. Inverse for play.
Change MPNowPlayingInfo dictionary, set new parameter
MPNowPlayingInfoPropertyPlaybackRate to 1.0f to show pause button
MPNowPlayingInfoPropertyPlaybackRate to 0.0f to show play button
see my answer how to use this code.
I am working on an app that needs to pause and unpause the audio stream coming from the ipod app. Is there any way to do that? I want to make a button that once you press it, it pauses the currently playing song on the ipod app. Pressing it again unpauses where the music left off. I am also working on a way to change the volume of the ipod app, is there anyway to do this that does not rely on the user manually moving a slider?
Any help at all would be great. Thanks!
you could use this framework if you want: Media Player Framework
Specially the MPMusicPlayerController Class
You have this:
Controlling Playback
– play
– pause
– stop
Managing Playback Mode and State
volume ( property )