cyclingIsBetter
cyclingIsBetter

Reputation: 17581

IOS: AVAudioPlayer stop and play

I have this code:

NSString *path = [NSString stringWithFormat:@"%@/%@",
                  [[NSBundle mainBundle] resourcePath],
                  @"change.mp3"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
change = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
[change prepareToPlay]; 

I write in this code in viewdidload; now with a button I start this sound, but if I press the same button, I want to stop this sound and restart the sound; I try this code inside IBAction but it don't work

[change stop];
[change prepareToPlay];
[change play];

what can I do?

Upvotes: 4

Views: 7658

Answers (3)

sch
sch

Reputation: 27506

The documentation of the method -stop says:

The stop method does not reset the value of the currentTime property to 0. In other words, if you call stop during playback and then call play, playback resumes at the point where it left off.

So you have to use the property currentTime and set it to 0:

[change pause];
[change setCurrentTime:0];
[change play];

Upvotes: 1

Till
Till

Reputation: 27597

How about this:

[change pause];
change.currentTime = 0.0;
[change play];

Upvotes: 13

GeRyCh
GeRyCh

Reputation: 1630

Did you try to make initialization again? I mean first [change release] and init it.

Upvotes: 0

Related Questions