簡體   English   中英

播放聲音時我的應用程序崩潰

[英]My Application crashes when playing a sound

我試圖單擊一個按鈕來播放聲音。 但是,當我運行該應用程序時,它崩潰了,我得到Thread 1 : signal SIGABRT

這是我的代碼:

.h文件

#import <AVFoundation/AVFoundation.h>

@interface HljodViewController : UIViewController <AVAudioPlayerDelegate>{

}

-(IBAction)playsound;

.m文件

-(IBAction)playsound {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Geese_4" ofType:@"mp3"];
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.numberOfLoops = 1;
    [theAudio play];
}

如果您不捕獲NSError沒人能告訴您如何解決您的問題。 我建議您執行以下操作:

NSError *outError = nil;
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&outError];
if (outError)
{
    NSLog(@"%@", [outError localizedDescription]);
}
... // continue on to happy time

嘗試這個:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourfile" ofType:@"mp3"];
NSError *error;
AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[sound play];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM