你能看到这段代码有什么问题吗?

Emi*_*mil 2 iphone xcode objective-c

NSString *path = [[NSBundle mainBundle] pathForResource:[arraySub objectAtIndex:indexPathHere.row] ofType:@"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.player = myPlayer;
[player prepareToPlay];
[player setDelegate:self];
[self.player play];

NSTimeInterval lenghtMusic = player.duration;

if (player.currentTime == lenghtMusic) {
 NSLog(@"It worked");
 [tableThing deselectRowAtIndexPath:indexPathHere animated:YES];
 [myPlayer autorelease];
 [file autorelease];
}
Run Code Online (Sandbox Code Playgroud)

你能看到什么问题吗?

显然,"if" - 陈述从未被称为......

wil*_*ood 17

如果我正确阅读了你的代码,你试图找出玩家何时完成游戏,然后你想要取消选择当前的游戏并释放玩家.您的问题是您启动播放器,然后立即检查它当前正在播放的位置.除非你有一个差不多0长度的文件,currentTime和length永远不会相等.

如果这是你想要做的,你应该使用AVAudioPlayerDelegate协议,特别是:

– audioPlayerDidFinishPlaying:successfully:
Run Code Online (Sandbox Code Playgroud)

播放器完成时调用的方法.

因此,您需要通过编辑.h文件来更改控制播放器的类,以使用AVAudioPlayerDelegate协议.显然,不管你以前做过什么.

@interface YourClass <AVAudioPlayerDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

在.m文件中:然后,当您创建并分配了播放器实例时:

[player setDelegate:self];
Run Code Online (Sandbox Code Playgroud)

在.m文件中也添加方法

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    // Your success code goes here
}
Run Code Online (Sandbox Code Playgroud)