(注:这里的代码的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