Xcode 按下新导航控制器时无法关闭声音播放器
我正在播放一个音频,可以通过以下方式打开和关闭 i操作//播放音频Xcode 按下新导航控制器时无法关闭声音播放器,xcode,ios5,object,avaudioplayer,Xcode,Ios5,Object,Avaudioplayer,我正在播放一个音频,可以通过以下方式打开和关闭 i操作//播放音频 NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; resourcePath = [resourcePath stringByAppendingString:@"/Apptrack.mp3"]; NSLog(@"Path to play: %@", resourcePath); NSError* err; //Initialize our player
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;
}
i关闭声音播放器的操作://停止音频
[player stop];
当我按下另一个视图控制器并再次返回时,按钮不再用于停止音频,它会持续运行(循环大约3分钟长)。因此,我尝试将其添加到上述代码中:
if ([self.player isPlaying] == YES) {
[self.player stop];
} else {
[self.player play];
}
还是没什么,也许有编程经验更丰富的人可以告诉我我做错了什么?好吧,找到了一个解决办法,我添加了
[player stop]代码>到按下下一个视图控制器按钮的iAction(仅用于更新)只有在按下下一个视图时播放音频时才会出现此问题。我把[玩家停止]在viewdidload中的code>试图在导航回原始视图控制器时停止音频播放,但仍然无效?出于某种原因,一旦音频播放且加载了下一个导航控制器,它将拒绝响应播放器停止
命令。