我嘗試了一切,我可以找到嘗試讓我的應用程序播放一個MP3。我有幾個2-3秒的MP3播放聲音效果,但我試圖播放15s MP3沒有任何運氣。播放15秒mp3 ios
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"test" withExtension: @"mp3"] error:&error];
if (error) {
NSLog(@"Error creating audio player: %@", [error userInfo]);
} else {
if([audioPlayer play] == FALSE) {
NSLog(@"Fail %@", error);
}
}
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 15.0 target: self
selector: @selector(stopMusic) userInfo: nil repeats: NO];
我懷疑計時器與它有什麼關係,但認爲我會包括它以防萬一。基本上我的錯誤檢查沒有被觸發。它「應該」正確播放,但沒有聲音出現,並且沒有顯示錯誤。
我已經嘗試了一些其他的事情來嘗試並得到這個工作,我已經嘗試了沒有計時器。我找不到一個體面的指南。我不需要一個完整的媒體播放器,只需要一些UIScreenViewController
的背景音樂。
我也嘗試了下面的代碼,它適用於短MP3,不適用於我想播放的更長的MP3。
NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"mp3"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
請幫忙,我花了數小時試圖對此進行排序。看來蘋果每次發佈操作系統時都會重新調整音頻。
您的播放器是否認爲它在播放?在if塊之後調用並記錄'[audioPlayer playing]'。也許與mp3文件有關?你的代碼看起來很適合我。 –
它打印真實,所以它認爲它正在播放。 – FaddishWorm