我正在播放可以通过以下方式打开和关闭的音频
IBAction //播放音频
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/Apptrack.mp3"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;
//Initialize our player pointing to the path to our resource
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
if( err ){
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
player.delegate = self;
[player play];
player.numberOfLoops = -1; //(for continuous loop use -1, or any number for number of loops)
player.currentTime = 1;
player.volume = .5;
}
IBAction 关闭声音播放器://停止音频
[player stop];
当我推到另一个视图控制器并再次返回时,该按钮不再用于停止音频并且它不断运行(循环大约 3 分钟长)。所以然后我尝试将其添加到上面的代码中:
if ([self.player isPlaying] == YES) {
[self.player stop];
} else {
[self.player play];
}
仍然没有,也许在编程方面更有经验的人可以指出我做错了什么的方向?