I have a button that plays an AVAudiplayer sound file every time I click it. The file is a relatively short .mp3 lazer sound. So, I declared an AVAUdiplayer, and prepeared it to play in the viewDidLoad funcition in my code. That worked fine. Then, I wrote [audioPlayer play] when the button gets clicked. This also worked fine. The problem is when I click the button repeatedly, the sound from the first button click does not cut off until it is done, even if there is overlap. So, the next sound doesn't play until the other one is finished. Answer with code please.
Check if its playing first. If it is stop it then play.
if (audioPlayer.rate != 0.0f) [audioPlayer stop];
[audioPlayer play];
Related
i would like to play sound in a loop, with AVAudioPlayer, just like in metronome app, from ios examples. I have the following code:
BOOL continuePlaying = YES;
while(continuePlaying){
[_audioPlayer play];
...do some other stuff
}
The problem is, that sound plays only once- during the first iteration. In next iterations play method doesn't seem to do anything. I would be grateful for any suggestions on what could be wrong.
PS: I know that AVAudiopoayer has numberOfLoops property, but in this case i want to do some other stuff between each plays, so i have to use a loop (i would like to create a metronome app).
Instead of playing while loop, capture delegate when ur sound finish playing
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
You can start playing sound again and do other stuff you would like to do.
Looping in case of playing sound seems illogical since you are firing multiple play and that wont work,
Check with this code, It worked for me:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
while (continuePlaying)
{
[player play];
//do your stuffs here
}
});
I am using AVAudioPlayer in my program, but having touble in something
I have 10 sounds, and as I click one to play or then others don't stop, I tried to solve by
[myPlayer stop];
myPlayer = nil;
it works, but takes time, so what should I do to stop when it click 2nd row on tableview for playing another sound?
If any query regarding question, you may ask in comments,
I think You hae added some observer before calling, [myPlayer play];... So you have to remove those observer
I'm writing my first iPad app that plays a video on a portion of the screen. My problem is that if the user changes to another view while the video is playing, the audio keeps playing in the background. I assume I have to add something to the "viewDidUnload" method but I'm not sure what to do. Any ideas? Thanks for any info.
You can try adding
[myMoviePlayer pause]; // assume myMoviePlayer is an instance variable
[myMoviePlayer stop];
myMoviePlayer = nil;
to your viewWillDisappear method.
For some reason, the expand button the arrow points to in the screenshot below causes the view controller that initiated video playback to animate back over top of the video, but without stopping video playback which means you can still hear the audio even though the video is no longer visible. I've tried other movie control styles, but there are other problems with those (for example, no controls causes the player to play the entire video before dismissing, i.e. no 'Done' button).
Here is the code that initiates the video playback:
player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[player setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[[self navigationController] presentModalViewController:player animated:YES];
[[player moviePlayer] play];
Any ideas/suggestions as to how I can either disable that button or receive its notification so I can respond accordingly?
Thanks.
I can't find a solution to this, but I did find a workaround. I simply call [player stop]; in my -viewDidLoad of the calling view controller. The outcome won't be what the user expects when they press that button, but it's better than allowing the video to continue to play when they press it.
In my custom movie player ,i use [player setInitialPlaybackTime:playSlider.value];
to make the movie resume playing at the time i slide to,but it has this situation:
the indicator returns to the start of the movie,then go to the time playSlider.value .At the same time, the view of movie get to the starting view of movie then resume the view at the time of playSlider.value, playback after buffering from the starting to the right time i slide to finally.
Actually,i do not like this situation,i'd like to let the indicator go to where i slide to directly,so does the view,and continue playback after buffering.
Is there any idea ?
thank you in advance.
I've solved it.It needed
[player setInitialPlaybackTime:playSlider.value];
[player stop];
[player play];