AVAudioPlayer问题

时间:2014-03-16 14:40:13

标签: ios iphone avaudioplayer

使用AVAudio Player播放超过1首歌曲

第一首歌结束时- (void)audioPlayerDidFinishPlaying:successfully:被叫。

如果我使用那个代码就行了

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSString *tempString = @"/";
    tempString  = [tempString stringByAppendingString:name];
    NSString *path = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] resourcePath]];
    path = [path stringByAppendingString:tempString];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error;

    AVAudioPlayer  *audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:url    error:&error];

    [AppDelegate getAppdelegate].audioPlayerForPlay = audioPlayer101;
    [[AppDelegate getAppdelegate].audioPlayerForPlay play];
} 

它有效

但是,如果我没有将实例放在音频播放器的perdefined属性中,它就不会播放。

这是代码

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSString *tempString = @"/";
    tempString  = [tempString stringByAppendingString:name];
    NSString *path = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] resourcePath]];
    path = [path stringByAppendingString:tempString];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSError *error;

    AVAudioPlayer  *audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:url    error:&error];
    [audioPlayer101  play];
} 

1 个答案:

答案 0 :(得分:0)

ARC代码

AVAudioPlayer  *audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:url    error:&error];
[audioPlayer101  play];

将编译为非ARC

AVAudioPlayer  *audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:url    error:&error];
[audioPlayer101  play];
[audioPlayer101 release];

audioPlayer101将在创建后立即释放,因为系统不会自动保留它并释放导致dealloc make play停止。

如果您使用instance.audioPlayerForPlay = audioPlayer101;代码,并且audioPlayerForPlay的属性为strong,则播放将按实例保留,因此播放可以继续播放。您可以尝试设置audioPlayerForPlay to be weak`,之前的代码也不会播放。