【发布时间】:2012-02-29 07:58:52
【问题描述】:
(注意:这里的代码是 Monotouch/C#,但是欢迎 Objective-C 回答!)
我使用 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()”了吗?
-
AVPlayer 上没有 PrepareToPlay()(我可以在 Apple 的文档中看到),只有 AVAudioPlayer。
标签: objective-c ios xamarin.ios avplayer