0

我正在播放可以通过以下方式打开和关闭的音频

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];
    }

仍然没有,也许在编程方面更有经验的人可以指出我做错了什么的方向?

4

1 回答 1

0

好的,我找到了一个解决方法,我添加[player stop];到按钮的 IBAction 推送到下一个视图控制器

于 2012-07-09T20:28:31.973 回答