(注:這裏的代碼的MonoTouch/C#,但是Objective-C的答案是歡迎!)本地文件使用AVAudioPlayer播放,但不使用AVPlayer?
進出口使用AVPlayer作爲我的應用程序播放的iPod庫文件以及本地MP3/M4A文件。 iPod庫文件的播放效果很好,但我似乎無法讓AVPlayer播放本地mp3文件。
例如,此代碼的工作:
NSUrl url;
AVAudioPlayer player2;
url = NSUrl.FromFilename(Path.Combine(Constants.TrackCacheLocation , songPath));
/* The url variable has the value file://localhost/private/var/mobile/Applications/B1ED2576-4398-43F3-8573-2FA3A0342265/Documents/Cache/Remote/dcaad4ff-452e-4d91-8788-c018e6b286f2.mp3 */
player2 = AVAudioPlayer.FromUrl(url);
player2.Play();
以上優良工程,請注意,這是使用AVAudioPlayer。
這不(使用AVPlayer):
NSUrl url;
AVPlayer player2;
url = NSUrl.FromFilename(Path.Combine(Constants.TrackCacheLocation , songPath));
/* The url variable has the value file://localhost/private/var/mobile/Applications/B1ED2576-4398-43F3-8573-2FA3A0342265/Documents/Cache/Remote/dcaad4ff-452e-4d91-8788-c018e6b286f2.mp3 */
player2 = AVPlayer.FromUrl(url);
player2.Play();
從本地或遠程Web服務器創建NSURL也可以(與AVPlayer),例如:http://10.0.0.1/testing/test.mp3延遲約一兩秒鐘,同時加載,然後開始罰款。我有一種感覺即時沒有正確創建我的NSUrl(即使它對AVAudioPlayer工作正常)。任何人有什麼想法可能會出錯?
另外,如果我檢查AVPlayer的CurrentItem.Status它仍然是未知的,它永遠不會變爲ReadyToPlay
您是否撥打了「PrepareToPlay()」? – 2012-02-29 15:23:17
AVPlayer上只有AVAudioPlayer沒有PrepareToPlay()(從Apple的文檔中可以看到)。 – Dermot 2012-03-01 07:12:48