How to change the background music as soon as the game starts? - ios

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.

Related

UIWebView Keep Video Playing After Exiting Fullscreen

In our app, we want to be able to keep a video playing after the user has quit fullscreen mode. The video is a YouTube one, thus it uses YTPlayerView framework, which uses UIWebView and iframe player to play the video.
As you have probably noticed, in iOS 11, when a web video enters fullscreen, there is only one 'X' button at the upper left corner to quit fullscreen and pause the video, instead of two(one 'Done' at the top and one arrow at the bottom) like before.
I have been able to get a UIWindowDidBecomeHidden notification when the fullscreen quits, and play the video programmatically, but then I realize that if the user has already paused the video manually before quitting fullscreen, it will start playing nonetheless when he/she quits. However, adding something in the YTPlayerDelegate won't do much since the system will pause the video(sending the stateDidChange delegate) before sending the notification, so I cannot differentiate between a manual pause when fullscreen and a system pause when quitting fullscreen.
My question is, is there a way to detect leaving fullscreen action before the system pauses the video? If not, is there a native API to have the player keep playing? If not either, is this feature possible? How would I achieve it? Thanks.

Magic Tap Pausing Background Audio

I'm trying to figure out how to respond to magic tap while my audio playback app is in the background. I've already done all the Command Center stuff and they work as expected, but while audio is playing, magic tap from the home screen or elsewhere doesn't work.
I'm able to respond to magic tap while my app is in the foreground. What am I missing here?

Hold and Release in Xcode (Sound)?

So, I just basically want to use AVFoundation Kit for playing my sound. I have the play method set, but I want to be able to hold down the button and play the sound that way rather than tapping it first. Once I release the button, I want the sound to stop. Simple: Hold down to play sound and then release your finger to stop the sound like a piano. Help please! Thanks.
If you right click on the button in IB you should be able to link the
Touch down
and
Touch Cancelled
button links so that it they both call the same method or they each call a different method one to stop the sound and one to start it.
you might also want to check out this
forum
hope this helps

MPMusicPlayerController: combine iPodMusicPlayer and applicationMusicPlayer to prevent continuing to next track

I have a bit of a strange problem. I have a music app that uses the [MPMusicPlayerController iPodMusicPlayer]. Everything is fine, notifications are fired for track changes and changes in playback state.
I have one screen where the user needs to review one single song, I don't want him to go on to the next song in his queue. Since there is no delegate method for when a track WILL change (only DID change), to prevent the music player from continuing to the next track I use a new [MPMusicPlayerController applicationMusicPlayer], give it iPodMusicPlayer's currently playing song and all is well. No new tracks to continue to, and I'm not touching the original iPodMusicPlayer queue so in theory, when I close this screen and use the iPodMusicPlayer again, all should be perfectly fine.
However, when the user is done on this screen and closes it, iPodMusicPlayer is now suddenly broken, notifications are not called and when I put the app to the background, music stops playing, causing me to believe that iPodMusicPlayer is now actually applicationMusicPlayer.
Okay so my question is basically: I need a way to prevent the music player to continue on to the next track in the queue. Switching to applicationMusicPlayer with one track seems to break stuff, as explained above. What's the best solution?
EDIT: because this might be a bit difficult to understand, I created a small project to show the problem: https://github.com/kevinrenskers/MPMusicPlayerControllerTest. Open the app while music is playing, see that the play button behaves correctly. Now open the popup, close it again and the play button is broken.
I found a solution to my problem: set the repeatMode to MPMusicRepeatModeOne and then catch the MPMusicPlayerControllerNowPlayingItemDidChangeNotification notification. You can stop the playback and you never continue to the next track. Once I'm done with the second screen I reset the repeatMode to the original value.

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