Iphone 音频层崩溃

Iphone 音频层崩溃,iphone,objective-c,memory-management,avaudioplayer,Iphone,Objective C,Memory Management,Avaudioplayer,我的iOS应用程序有一个问题。我想使用iAction和AVAudioPlayer播放多个音频文件(.WAV)。不幸的是,声音会播放,但是如果我多次播放声音,我的应用程序就会崩溃。可能是因为内存没有正确释放。你能证实我的假设和/或告诉我如何解决这个问题吗 这是我的密码 ViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVAudioPlayer.h> @interface ViewControll

我的
iOS
应用程序有一个问题。我想使用
iAction
AVAudioPlayer
播放多个音频文件(
.WAV
)。不幸的是,声音会播放,但是如果我多次播放声音,我的应用程序就会崩溃。可能是因为内存没有正确释放。你能证实我的假设和/或告诉我如何解决这个问题吗

这是我的密码

ViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface ViewController : UIViewController
{
    NSString        *Path;
}

- (IBAction)Sound1;
- (IBAction)Sound2;
- (IBAction)Sound3;
- (IBAction)Sound4;

@end
#导入
#进口
@界面ViewController:UIViewController
{
NSString*路径;
}
-(i)声音1;
-(i)声音2;
-(i)声音3;
-(i)声音4;
@结束
ViewController.m

#import <AVFoundation/AVAudioPlayer.h>
#import "ViewController.h"

@implementation ViewController

- (IBAction)Sound1
{
    Path = [[NSBundle mainBundle] pathForResource:@"Sound1" ofType:@"wav"];
    Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
    [Media play];
}

- (IBAction)Sound2
{
    Path = [[NSBundle mainBundle] pathForResource:@"Sound2" ofType:@"wav"];
    Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
    [Media play];
}

- (IBAction)Sound3
{
    Path = [[NSBundle mainBundle] pathForResource:@"Sound3" ofType:@"wav"];
    Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
    [Media play];
}

- (IBAction)Sound4
{
    Path = [[NSBundle mainBundle] pathForResource:@"Sound4" ofType:@"wav"];
    Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
    [Media play];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [player release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)dealloc
{
    [super dealloc];
}

@end
#导入
#导入“ViewController.h”
@实现视图控制器
-(i)声音1
{
Path=[[NSBundle mainBundle]pathForResource:@“Sound1”,类型为:@“wav”];
Media=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:Path]错误:NULL];
[媒体播放];
}
-(i)声音2
{
Path=[[NSBundle mainBundle]pathForResource:@“Sound2”类型:@“wav”];
Media=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:Path]错误:NULL];
[媒体播放];
}
-(i)声音3
{
Path=[[NSBundle mainBundle]pathForResource:@“Sound3”类型:@“wav”];
Media=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:Path]错误:NULL];
[媒体播放];
}
-(i)声音4
{
Path=[[NSBundle mainBundle]pathForResource:@“Sound4”类型:@“wav”];
Media=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:Path]错误:NULL];
[媒体播放];
}
-(无效)AudioPlayerDifinishPlaying:(AVAudioPlayer*)玩家成功:(BOOL)标志
{
[玩家释放];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
}
-(无效)视图卸载
{
[超级视频下载];
}
-(无效)解除锁定
{
[super dealoc];
}
@结束

媒体从未发布过。。。这导致内存泄漏。使用SetTargeter方法,这样每当分配新对象时,前一个对象都将被释放。

媒体永远不会被释放。。。这导致内存泄漏。使用SetTargeter方法,这样无论何时分配新对象,前一个对象都将被释放。

问题是您分配了大量AVAudioPlayer实例,但没有释放任何实例

您需要做的是将类设置为
AVAudioPlayerDelegate
,从类中删除媒体实例变量(只需在各种iAction方法中创建一个新的实例变量),并按如下方式成功实现
audioplayerdfinishplaying:successfully:
方法:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [player release];
}
...
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
...
最后,更新代码,将您的类设置为您设置的每个AvaudioPlayer的委托,如下所示:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [player release];
}
...
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
...

通过这样做,您将确保释放您创建的所有AVAudioPlayer实例。

问题是您分配了大量AVAudioPlayer实例,但没有释放任何实例

您需要做的是将类设置为
AVAudioPlayerDelegate
,从类中删除媒体实例变量(只需在各种iAction方法中创建一个新的实例变量),并按如下方式成功实现
audioplayerdfinishplaying:successfully:
方法:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [player release];
}
...
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
...
最后,更新代码,将您的类设置为您设置的每个AvaudioPlayer的委托,如下所示:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [player release];
}
...
Media = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Path] error:NULL];
[Media setDelegate:self];
[Media play];
...

通过这样做,您将确保释放您创建的所有AVAudioPlayer实例。

您好!好的,我从类中删除了“媒体”实例并粘贴了您的代码,但应用程序仍然崩溃:C@Aluminum假设您已经从de alloc?Alumina My bad中删除了(现在是多余的)版本-您还需要将自己设置为AVAudioPlayer的代理-我将更新我的答案。@Alumina已更新。我很想在你情绪失控之前检查一下它是否有效。:-)@铝合金非常好。快乐编码::-)你好好的,我从类中删除了“媒体”实例并粘贴了您的代码,但应用程序仍然崩溃:C@Aluminum假设您已经从de alloc?Alumina My bad中删除了(现在是多余的)版本-您还需要将自己设置为AVAudioPlayer的代理-我将更新我的答案。@Alumina已更新。我很想在你情绪失控之前检查一下它是否有效。:-)@铝合金非常好。快乐编码::-)