我想通過AVAudioPlayer
播放MP3,我認爲這很簡單。不幸的是,它不是很有效。下面是我所做的:AVAudioPlayer立即停止播放ARC
- 爲了測試的緣故,我創建了一個新的iOS應用程序(單 視圖)在Xcode。
我加入了AVFoundation框架到項目還有
#import <AVFoundation/AVFoundation.h>
到ViewController.m
我增加了一個MP3文件到Apps的文檔「文件夾。
我改變了
ViewControllers
viewDidLoad:
以下幾點:
代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString* recorderFilePath = [NSString stringWithFormat:@"%@/MySound.mp3", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recorderFilePath] error:nil];
audioPlayer.numberOfLoops = 1;
[audioPlayer play];
//[NSThread sleepForTimeInterval:20];
}
不幸的是,音頻明顯停止它開始播放之後。如果我取消對sleepForTimeInterval
的評論,它會播放20秒,然後停止播放。只有使用ARC進行編譯時纔會出現此問題,否則,它會完美地工作。
你不需要啓動另一個線程來播放音頻。你有沒有試過提供一個錯誤指針來查看是否有任何東西被記錄? – isaac
你會得到什麼錯誤? – Daniel
是的,我通過一個NSError initWithContensOfURL。它保持零,但...所以我沒有得到任何錯誤,它只是立即停止播放。 – Phlibbo