replacescene stops background music in my game cocos2d ios - ios

I am writing a code for a game in cocos2d. I has 5 CCScenes.I am trying to implement background music in my game. I am playing a same file in all the CCScenes. and I have implemented all the volume related tasks in a singleton class called as controller.
so my problem is whenever I navigate from one scene to another scene, the background music starts but as soon as new page is loaded it stops.
I want the music to be played continuously Can anyone help me?
here is my code that plays my sound file-
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"main_game_bg.mp3"];
[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume: [[Controller sharedMySingleton] getBgVolume]];
I have written these two lines in my main menu class. these play the sound on main menu screen as well as when i navigate to screen 1st. but as soon as i go to next screen i.e. screen2, it restarts music and after screen 2 is loaded, it stops the music. and same happens with next screens.

[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"blues.mp3" loop:YES];
[[SimpleAudioEngine sharedEngine] playEffect:#"alien-sfx.caf"];
You forgot to mention loop in the first line

Related

Playing short sound with SystemSound or AVAudioPlayer on iOS

Creating custom spinner for iOS and for each rotation change need to play sound. Sound itself is 0.5 sec. According to Apple's documentation I used SystemSound to play it but now I can't control volume with hardware buttons cause audio context is Volume(media) and as I understood SystemSound works in different context, Ringer.
I tried to use AVAudioPlayer but there is a small delay when sound is attaching to the player and this glitches UI(small delay so spinner doesn't rotate smoothly).
Any ideas how this can be solved ?
I was thinking about somehow programmaticaly change this context by triggering controls that are working with systemSound, but no luck.
EDITED:
I've made sure that have only 1 instance of AVAudioPlayer and didn;t stop playing sound but paused it and only after spinner finished rotating then stop and dispose, but still same result

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 to change the background music as soon as the game starts?

Hey so I am playing some background music as soon as my app starts. Like a menu song, and it is looping forever throughout the game. I would like to change the music as soon as you hit the start game button. Just so I can have a different menu and game songs. I tried
backgroundMusicPlayer.play()
playBackgroundMusic("MUSIC.wav")
I did this in my touchesBegan method but of course every time you touch the screen the song is going to start over and over again. Any ideas how I can do this? Thank you in advance
While the game starts,if a user touches the initial screen,then it could be identified with a tapGestureRecognizer,in the corresponding class file.
I prefer tap recognising than touchesBegan ,so that, when user touches the screen ,you could play the music via
backgroundMusicPlayer.play()
playBackgroundMusic("MUSIC.wav")
and stop this by backgroundMusicPlayer.stop()when moving on to MENU page.
Then the music wont get played everytime user touches the screen.
I hope this works.
Simply use AVFoundation. Follow this basic tutorial to play audio as soon as the ViewController loads, then on clicking the start button stop the audioPlayer.

How can you check if SKAction is already playing a file?

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;
}

Playing sections of a movie

Is it possible to play sections of a movie without using timers to do the pausing and unpausing? For example I have a movie loaded into MPMoviePlayerController, the user taps and the first section of the movie is played then the movie is paused, on the second tap another section is played and so on. Setting the initialPlayBackTime and endPlayBackTime of MPMoviePlayerController doesn't help as endPlayBackTime gets updated only when stop is called (could it be a bug?), but stopping causes an annoying flicker.
I don't think so... You need the timer and that's it.

Resources