AVAudioPlayer 泄漏并发出警告,最后我的应用程序崩溃了

发布于 2024-11-05 16:51:03 字数 456 浏览 1 评论 0原文

我正在使用 AVAudioPlayer,我再次播放声音并再次在我的应用程序中调用此方法 100 次。问题是我总是 alooc 它,但当我要释放时,声音不起作用。 我该怎么办。

-(void) ButtonSound
{

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1" 
         ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[player play];
[fileURL release];

}

内存泄漏并且应用程序崩溃,我想立即分配播放器并一次又一次地使用它。

I am using AVAudioPlayer and i am plying sound again and agian and call this method 100 times in my application.The problem is that i always alooc it but when i am going to release, sound does not work.
what will i do.

-(void) ButtonSound
{

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1" 
         ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[player play];
[fileURL release];

}

memory is leak and the application is crashed, I want to allocate player at once and use it again and again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终难遇 2024-11-12 16:51:03

执行类似的操作

您可以在头文件中

AVAudioPlayer *buttonSoundPlayer;

,然后在实现中声明播放器

-(void)playButtonSound {
    if(buttonSoundPlayer == nil) {
       NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1" ofType:@"mp3"];
       NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
       buttonSoundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
       [fileURL release];
    }
    [buttonSoundPlayer play];
}

You can do it something like this

on you header file, declare the player already

AVAudioPlayer *buttonSoundPlayer;

then on the implementation

-(void)playButtonSound {
    if(buttonSoundPlayer == nil) {
       NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1" ofType:@"mp3"];
       NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
       buttonSoundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
       [fileURL release];
    }
    [buttonSoundPlayer play];
}
鲸落 2024-11-12 16:51:03

将代码更改为:

-(void) ButtonSound
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1" 
         ofType:@"mp3"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    if (player) {
        [player release];
    }
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    [player play];
    [fileURL release];
}

您还需要在 dealloc 方法中释放 player 。您可能还需要检查单击按钮时 player 是否已经在播放,如果是,则可能跳过此方法。

您可能还需要保留您的播放器对象,但这取决于您声明它的方式(示例中未显示)。

Change your code to this:

-(void) ButtonSound
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Button1" 
         ofType:@"mp3"];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    if (player) {
        [player release];
    }
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    [player play];
    [fileURL release];
}

You also need to release player in your dealloc method. You also probably need to check whether player is already playing when you click the button, and perhaps skip this method if so.

You may also need to retain your player object, but that depends on how you've declared it (not shown in your sample).

演多会厌 2024-11-12 16:51:03
-(void) ButtonSound
{

    NSBundle        *mainBundle = [NSBundle mainBundle];
    NSError         *error;
    NSURL           *audioURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"Button1" ofType: @"mp3"]];
    AVAudioPlayer   *player1 = [(AVAudioPlayer*) [AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
    self.player =   player1;
    [self.player play];
    [player1 release];
}

这段代码没有错误,我使用这段代码没有发现任何泄漏

-(void) ButtonSound
{

    NSBundle        *mainBundle = [NSBundle mainBundle];
    NSError         *error;
    NSURL           *audioURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"Button1" ofType: @"mp3"]];
    AVAudioPlayer   *player1 = [(AVAudioPlayer*) [AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
    self.player =   player1;
    [self.player play];
    [player1 release];
}

This code is error free and i have not found any leaks by using this code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文