Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer resetting currently playing sound and playing it from beginning

I'm having an issue using AVAudioPlayer where I want to reset a player if it's currently playing and have it play again.

I try the following with no luck:

The sound plays once but then the second time i select the button it stops the sound, the third time starts the sound up again.

//Stop the player and restart it
if (player.playing) {
    NSLog(@"Reset sound: %@", selectedSound);
    [player stop];
    [player play];
} else {
    NSLog(@"playSound: %@", selectedSound);
    [player play];      
}

I've also tried using player.currentTime = 0 which indicates that would reset the player, that didn't work, I also tried resetting currentTime = 0 and then calling play that didn't work.

//Stop the player and restart it
if (player.playing) {
    NSLog(@"Reset sound: %@", selectedSound);
    player.currentTime = 0;
    [player play];
} else {
    NSLog(@"playSound: %@", selectedSound);
    [player play];      
}
like image 918
Dougnukem Avatar asked Jul 21 '09 19:07

Dougnukem


1 Answers

For your situation I would try the following

//Pause the player and restart it
if (player.playing) {
    NSLog(@"Reset sound: %@", selectedSound);
    [player pause];
}

player.currentTime = 0;
[player play];

If you are going to immediately play the sound again, I would suggest pause instead of stop. Calling stop "Stops playback and undoes the setup needed for playback."

like image 192
Jonathan Arbogast Avatar answered Nov 17 '22 00:11

Jonathan Arbogast