(注意:这里的代码是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
1 回答
许多使用
NSUrl
或NSUrlRequest
参数的API设计为 async ,如果定义为局部变量,则会出现问题(or even crash)(例如,当本机代码仍需要您的方法返回时,将收集该问题) .您上面的代码没有提供有关它们创建位置的足够信息 . 如果你重新分配它们,直到你不再需要
AVPlayer
为止 .