2

我有一个子类,AVAudioPlayer在该子类中我有一个停止当前播放器的方法,并且(出于我不会解释的原因)audioPlayerDidFinishPlaying像这样手动调用:

// Handles stopping the player and calling audioPlayerDidFinishPlaying
- (void) stopPlayerForTimedRepeat {

    // Stop the player
    [self stop];

    // Manually call the audio player callback
    EditPlayListViewController *playlistController = [[EditPlayListViewController alloc] init];
    [playlistController audioPlayerDidFinishPlaying:self successfully:YES];
    [playlistController release];

}

但是,当我audioPlayerDidFinishPlaying像这样手动调用时,我原来的所有变量EditPlaylistViewController都超出了范围。

如何避免这种情况,以便我仍然可以访问所有原始变量?

4

2 回答 2

2

我想出了一个更好的方法来做到这一点,而无需手动调用audioPlayerDidFinishPlaying,这样所有的变量仍然在范围内。

// Handles stopping the player and calling audioPlayerDidFinishPlaying
- (void) stopPlayerForTimedRepeat {

    // Fast forward the call to the end, which will also call audioPlayerDidFinishPlaying
    [self setCurrentTime:[self duration]];

}
于 2012-06-09T06:38:18.563 回答
0

变量范围丢失,因为您 EditPlayListViewController *playlistController = [[EditPlayListViewController alloc] init];在停止方法后在方法中创建了一个新对象。

.h请在您的文件中声明播放器(像这样并在您的方法EditPlayListViewController *playlistController;中分配它(例如.viewDidLoadplaylistController = [[EditPlayListViewController alloc] init];

像这样改变你的方法,

- (void) stopPlayerForTimedRepeat
{
    // Stop the player
     [self stop];

    // Manually call the audio player callback
    [playlistController audioPlayerDidFinishPlaying:self successfully:YES];
    [playlistController release];

}

希望它对你有用,如果没有,请告诉我。

于 2012-06-09T14:12:18.167 回答