0

I'm using iOS 5 with ARC enabled. I'm playing a recording associated with a particular view when the view is tapped. When the user changes views, the playback stops. I used NSNotification to call the -stopPlayback method which works fine. The problem is that when the user returns to the original view and taps it, it continues where the recording left off.

With ARC disabled I could use the following code:

-stopPlayback
{
 if([audioPlayer isPlaying])
 {
 [audioPlayer stop];
 [audioPlayer release];
 audioPlayer = nil;
 }
}

Since ARC doesn't allow [audioPlayer release], I can't reset the playback from the beginning.I removed the [audioPlayer release] and audioPlayer=nil; since ARC will handle it and the result is the recording plays where it left off after the previous stop. I'm not seeing any good examples or documentation from Apple on how to restart the recording from the beginning when playback occurs. What is the best way to proceed?

J1NX3D
  • 1
  • 4

2 Answers2

0

You still need the setting to nil:

if([audioPlayer isPlaying])
{
    [audioPlayer stop];
    audioPlayer = nil;
}
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • Thanks. I did update that in my code. What about the restarting playback from the beginning? – J1NX3D Nov 09 '11 at 19:29
  • What about currentTime? If I add 'audioPlayer.currentTime=0;' would that restart the playback from the beginning? – J1NX3D Nov 09 '11 at 19:32
  • If all you want is restarting the player then you don't need to release it. See [this question](http://stackoverflow.com/questions/1161148/avaudioplayer-resetting-currently-playing-sound-and-playing-it-from-beginning). – DarkDust Nov 09 '11 at 20:23
0

This is the solution I came up with:

-stopPlayback{  
 if([audioPlayer isPlaying])
 {
   [audioPlayer stop];
   audioPlayer.currentTime=0;
 }
}

The recording now replays from the start no matter when it was stopped. I tried using [audioPlayer pause] instead of [audioPlayer stop] which caused the playback not to play again after the initial pause. A similar thing happened when I added 'audioPlayer=nil; (although I didn't document the exact combination). The related question posted as part of the response did utilize currentTime=0 but the rest of the code did not meet my needs. Can anyone see why this would not work? Any further comments appreciated.

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
J1NX3D
  • 1
  • 4