OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何在 ios5 中播放本地方法的 AVAudioPlayer? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:22
标题: iphone - 如何在 ios5 中播放本地方法的 AVAudioPlayer?

我认为它与 iOS 5 中的 ARC 有关,但是当我在方法中声明一个 AVAudioPlayer 并播放它时,它不会播放。我认为它会在方法结束时自动释放,因为我不记得在 iOS5 之前发生过这种情况。如果我将其设为全局变量,则它可以正常工作。有什么方法可以使用本地实例?



Best Answer-推荐答案


将其设为保留属性,并将您的类设为委托(delegate),以便在播放完毕后进行清理。在您的标题中:

@interface MyClass : NSObject <AVAudioPlayerDelegate>
@property(strong)AVAudioPlayer *player;

那么,当你使用它时:

self.player = [AVAudioPlayer alloc] initWithContentsOfURL:URL error:&err];
[player setDelegate:self];

并在委托(delegate)方法中清理它:

-(void)audioPlayerDidFinishPlayingAVAudioPlayer *)player successfullyBOOL)playedSuccessfully {
    self.player = nil;
}

将它作为属性的一个好处是,如果需要,您可以中断它:

-(void)someInterruptionOccurred {
    [self.player stop];
}

关于iphone - 如何在 ios5 中播放本地方法的 AVAudioPlayer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7810936/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4