变量超出范围

时间:2012-06-09 06:01:07

标签: iphone objective-c ios scope avaudioplayer

我有一个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中的所有变量都超出了范围。

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

2 个答案:

答案 0 :(得分: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]];

}

答案 1 :(得分:0)

变量作用域丢失是因为您在停止后在方法中创建了一个新对象EditPlayListViewController *playlistController = [[EditPlayListViewController alloc] init];

请在.h文件中声明播放器(例如此EditPlayListViewController *playlistController;)并使用viewDidLoad方法分配(例如playlistController = [[EditPlayListViewController alloc] init];

并改变你的方法,

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

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

}

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