Make sound stop when button is pressed iOS - iphone

I have multiple buttons that play sounds using AVAudioPlayer. If you press one, and press another before the first ends, they are both playing at the same time. How would i make it so if you press two buttons, only the last pressed button's sound plays?

I don't know the exact code, but you may have to bool something or try AudioServicesDisposeSystemSoundID. It will stop the sound immediately.

Before you start the new song you have to do:
[song1 stop];
song1.currentTime = 0;
And do that for all the songs that could be possibly playing. Then you can make the new song play. (The current time sets it to start from the beginning if you play it again.) hope that helps :)

Related

How to tell the MPNowPlayingInfoCenter wether or not the music is playing or paused?

I can't seem to make iOS show the correct play / pause button in the remote audio controls. I do receive the remote control events and set all values of the nowPlayingInfo dictionary.
Everything works fine and I even see a cover photo on the lock screen. Except the pause/play button. It always looks like pause even if my AVAudioPlayer is playing. It sends a pause event regardless of playback state.
How can I notify iOS that AVAudioPlayer is paused and that it should now show a play button in the remote control buttons bar?
Make sure that you're setting the MPNowPlayingInfoPropertyPlaybackRate property. 0.0f to indicate paused, 1.0f to indicate playing. You'll also need to set the MPNowPlayingInfoPropertyElapsedPlaybackTime when you change these values.
Here is example code where updateMetadata is a function that applies those changes to the MPNowPlayingInfoCenter.nowPlayingInfo dictionary. This would indicate to the center that the player is paused.
[self updateMetadata:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:audioFile.player.currentTime],
MPNowPlayingInfoPropertyElapsedPlaybackTime,
[NSNumber numberWithFloat:0.0f],
MPNowPlayingInfoPropertyPlaybackRate,
nil]];
I had this problem today: I found that the fact that I was recording audio as well as playing it caused the button to show the pause symbol.
When I stopped the AVAudioRecorder from recording the pause button became a play button.
Quite often, the problem is simply the iPhone Simulator. As soon as you are using the play() function of your AVAudioPlayer instance, the remote control bar is supposed to toggle pause/play automatically. If you run into problems where this doesn't happen, try to run your program on a device.
To toggle the button, you do not need to set any playingInfo of the MPNowPlayingInfoCenter, neither do you need to hold an active AVAudioSession.
Here is example code where updateMetadata is a function that applies those changes to the MPNowPlayingInfoCenter.nowPlayingInfo dictionary. This would indicate to the center that the player is paused.In swift
self.updateMetadata(NSDictionary.dictionaryWithValuesForKeys(NSNumber(Double(audioFile.player.currentTime))),MPNowPlayingInfoPropertyElapsedPlaybackTime,NSNumber(numberWithFloat:0.0f),MPNowPlayingInfoPropertyPlaybackRate,nil)

How do I make MPMusicPlayerController register changes in playlist?

I'm trying to build a music player app in xcode, but I'm having trouble when changing the playlist (a.k.a. the music player queue). Any time I change something in my playlist (i.e. when addind songs or changing song order, or when pressing the "next" button, etc.) I always seem to have to execute these tow lines of code:
[self.musicplayer play];
[self.musicplayer pause];
apparently, the music player only really registers changes when i send it the "play" command; soon after, i have to send it "pause", so that the song won't actually start playing.
without these lines of code, i would have the following problem (among others):
I would have, say, 6 songs, named "1" through "6". song 1 was playing, but i paused it. I then pressed the "next button" 3 times, which executed the following code (3 times):
[self.musicPlayer skipToNextItem];
therefore, by my logic, the music player should have song number 4 as its current playing song; but when I pressed play, song number 1 would start playing, as if it hadn't noticed I pressed "next".
I was only able to solve the problem by telling it to play and pause, as described above, but that was a very ugly solution, to say the least. also, in some cases, the ipod would play the first seconds of the song before processing it had to pause right away, and that was pretty bad :p
Does anyone know the correct way I should be doing this?
I am using Xcode 4.2.5 preview, and testing it on an iphone 4s with ios 6.

Know when song is done playing

In my app I have a few buttons that play songs when pressed. When a song is done, I want to change the image on the play/stop button to the play image. I know I could set a timer to check to make sure the right image is displayed, but I feel like this would be a waste of system resources. Is there a way to know when a song is done playing or something else? (Also, I'm using the AVAudioPlayer)
-Thanks
Set up a delegate, and respond to the audioPlayerDidFinishPlaying callback.

Is it possible to play AVQueuePlayer in reverse order?

I am using AVQueuePlayer and it is working fine.
My problem now is that I have a back button. If I press the button, previous video needs to be played. Is there any way for that?
Thanks.

iPhone how to stop MPMoviePlayerController is closing at the end?

In my iPhone app, I'm using MPMoviePlayerController to play a movie. I'm hiding all the controls that are by default visible on the movie player. But I placed a "Replay" button over the player control.
At the end of movie, the player is being removed. But I want to stop at the last frame, so that when I click "Replay" button, it will start from beginning. I wrote the functionality for replay and its working good. When ever the movie is playing and on click of "Replay", its restarting the video from starting.
The problem I'm facing is that, at the end of movie its becoming white screen and pressing "Replay" button is not restarting the movie. How to handle this situation?
Its not necessary to create new instance. I found out the solution. Don't just release the instance of player. When replay button is clicked (either in middle of video or after completion), just pause it, move the location to beginning and play it. That's it.. no retain, no release nothing.... Its upto the programmer/developer when to release the player and remove it from the view.
I never used MPMoviePlayerController but pherhaps it's instance is released when the movie has ended. In this case incrementing the retaincounter and manually releasing it would solve the problem.