0

在我的应用程序中,我使用 AVAudioPlayer 播放音频;一切都好,它可以正常工作几分钟,但在某些时候它会崩溃,这是我的代码和控制台输出,但我不明白如何解决我的问题......

我有这个代码:

- (void) viewDidLoad{
    [super viewDidLoad];

    soundID = [[AVAudioPlayer alloc]init];
}

- (void)playAudioReturnLettera{
    NSString *path = [NSString stringWithFormat:@"%@/%@",
                      [[NSBundle mainBundle] resourcePath],
                      @"returnObject.mp3"];
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
    [soundID initWithContentsOfURL:filePath error:nil];
    soundID.delegate = self; //<-- here I have the problem (Thread 1)
    [soundID prepareToPlay]; 
    [soundID play];

}

这是控制台中的输出:

10:13:07.639 <0x3fbdad98> shm_open failed: "AppleAudioQueue.100.778" (23) flags=0x2 errno=24

2012-04-26 10:13:07.643 Game[155:418b] 10:13:07.643 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.779" (23) flags=0x2 errno=24

2012-04-26 10:13:07.655 Game[155:418f] 10:13:07.655 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.780" (23) flags=0x2 errno=24

2012-04-26 10:13:07.664 Game[155:4193] 10:13:07.664 <0x2fffd000> shm_open failed: 
"AppleAudioQueue.100.781" (23) flags=0x2 errno=24

2012-04-26 10:13:07.680 Game[155:4197] 10:13:07.680 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.782" (23) flags=0x2 errno=24

2012-04-26 10:13:07.696 Game[155:419b] 10:13:07.696 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.783" (23) flags=0x2 errno=24

2012-04-26 10:13:07.711 Game[155:419f] 10:13:07.711 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.784" (23) flags=0x2 errno=24

2012-04-26 10:13:07.727 Game[155:41a3] 10:13:07.727 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.785" (23) flags=0x2 errno=24

2012-04-26 10:13:07.744 Game[155:41a7] 10:13:07.744 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.786" (23) flags=0x2 errno=24

2012-04-26 10:13:07.759 Game[155:41ab] 10:13:07.759 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.787" (23) flags=0x2 errno=24

2012-04-26 10:13:07.775 Game[155:41af] 10:13:07.775 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.788" (23) flags=0x2 errno=24

2012-04-26 10:13:07.792 Game[155:41b3] 10:13:07.793 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.789" (23) flags=0x2 errno=24

2012-04-26 10:13:07.807 Game[155:41b7] 10:13:07.807 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.790" (23) flags=0x2 errno=24

2012-04-26 10:13:07.823 Game[155:41bb] 10:13:07.823 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.791" (23) flags=0x2 errno=24

2012-04-26 10:13:07.840 Game[155:41bf] 10:13:07.841 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.792" (23) flags=0x2 errno=24

2012-04-26 10:13:07.857 Game[155:41c3] 10:13:07.857 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.793" (23) flags=0x2 errno=24

2012-04-26 10:13:07.873 Game[155:41c7] 10:13:07.873 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.794" (23) flags=0x2 errno=24

2012-04-26 10:13:07.890 Game[155:41cb] 10:13:07.890 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.795" (23) flags=0x2 errno=24

2012-04-26 10:13:07.905 Game[155:41cf] 10:13:07.906 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.796" (23) flags=0x2 errno=24

2012-04-26 10:13:07.921 Game[155:41d3] 10:13:07.921 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.797" (23) flags=0x2 errno=24

2012-04-26 10:13:07.938 Game[155:41d7] 10:13:07.938 <0x2fffd000> shm_open failed: "AppleAudioQueue.100.798" (23) flags=0x2 errno=24

2012-04-26 10:13:10.478 Game[155:707] *** -[AVAudioPlayer setDelegate:]: message sent to deallocated instance 0x546bb50
4

2 回答 2

1

initWithContentsOfURL:error:您正在对已初始化的对象 ( )调用初始化方法 ( ),soundID而不存储该方法返回的对象。

将分配和初始化保持在一起(并且只初始化一次)是一个好主意,因此您的代码应如下所示:

- (void) viewDidLoad{

    [super viewDidLoad];

    NSString *path = [NSString stringWithFormat:@"%@/%@",
                     [[NSBundle mainBundle] resourcePath],
                     @"returnObject.mp3"];
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
    soundID = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
    soundID.delegate = self;
}

- (void)playAudioReturnLettera{
    [soundID prepareToPlay]; 
    [soundID play];
}
于 2012-04-26T09:02:43.373 回答
0

试试这个代码..这对你有用。不要忘记添加AVAudioPlayerDelegate您的 .h 文件。

- (void) viewDidLoad{

    [super viewDidLoad];

}

- (void)playAudioReturnLettera{
    NSString *path = [NSString stringWithFormat:@"%@/%@",
                     [[NSBundle mainBundle] resourcePath],
                     @"returnObject.mp3"];
    NSURL *filePath = [NSURL fileURLWithPath:path];
    soundID = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
    soundID.delegate = self;
    [soundID play];
}
-(void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"Audio Player Successfully played");
}

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{

    NSLog(@"Decode Error occurred");
}
于 2012-04-26T13:41:17.593 回答