Objective-c:如何为AvAudioPlayer释放内存

时间:2023-01-02 20:01:39

I have a sound file that I use throughout my program:

我有一个声音文件,我在整个程序中使用:

NSString *soundPath1 = [[NSBundle mainBundle] pathForResource:@"mysound" ofType:@"mp3"];
NSURL *soundFile1 = [[NSURL alloc] initFileURLWithPath:soundPath1];   
soundFilePlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile1 error:nil];
[soundFilePlayer1 prepareToPlay];   

So soundFilePlayer1 is defined in my .h file because I need it to be global.

所以soundFilePlayer1在我的.h文件中定义,因为我需要它是全局的。

Now... do I need to release soundFilePlayer1 and soundFile1? (and soundPath1?)

现在......我需要释放soundFilePlayer1和soundFile1吗? (和soundPath1?)

If so... where do I do this? I think I need to release soundFilePlayer1 in: (but do I need to stop it first since it may be playing when they exit the program?)

如果是的话......我在哪里这样做?我想我需要释放soundFilePlayer1 :(但我是否需要先停止它,因为它们可能在退出程序时播放?)

- (void)dealloc {
   [soundFilePlayer1 release];
}

what about the other files... where do I release those? Thanks

其他文件怎么样......我在哪里发布这些文件?谢谢

1 个解决方案

#1


1  

Remember a simple rule of thumb. Only release if you have called alloc, init, or new on the object. So, you'll want to release soundFile1. Alternatively, you can use a convenience method and have the sound file added to the autorelease pool automatically:

记住一个简单的经验法则。只有在对象上调用了alloc,init或new时才会释放。所以,你想发布soundFile1。或者,您可以使用便捷方法并将声音文件自动添加到自动释放池中:

NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
NSURL *file = [NSURL fileURLWithPath:path]; //autoreleased
player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];

if([player prepareToPlay]){
    [player play];
}  

And yes, you can release player (soundFilePlayer1) in dealloc, but if you're not looping the audio file there's a better way. Conform to the AVAudioPlayerDelegate:

是的,您可以在dealloc中释放播放器(soundFilePlayer1),但如果您没有循环播放音频文件,则有更好的方法。符合AVAudioPlayerDelegate:

@interface YourView : UIViewController <AVAudioPlayerDelegate> { //example

Then implement the method here:

然后在这里实现方法:

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    //your audio file is done, you can release safely
    [player release];
}

#1


1  

Remember a simple rule of thumb. Only release if you have called alloc, init, or new on the object. So, you'll want to release soundFile1. Alternatively, you can use a convenience method and have the sound file added to the autorelease pool automatically:

记住一个简单的经验法则。只有在对象上调用了alloc,init或new时才会释放。所以,你想发布soundFile1。或者,您可以使用便捷方法并将声音文件自动添加到自动释放池中:

NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
NSURL *file = [NSURL fileURLWithPath:path]; //autoreleased
player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];

if([player prepareToPlay]){
    [player play];
}  

And yes, you can release player (soundFilePlayer1) in dealloc, but if you're not looping the audio file there's a better way. Conform to the AVAudioPlayerDelegate:

是的,您可以在dealloc中释放播放器(soundFilePlayer1),但如果您没有循环播放音频文件,则有更好的方法。符合AVAudioPlayerDelegate:

@interface YourView : UIViewController <AVAudioPlayerDelegate> { //example

Then implement the method here:

然后在这里实现方法:

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    //your audio file is done, you can release safely
    [player release];
}