[英]Get mp3 as NSData
I want to play a mp3 from my server with AVAudioPlayer. 我想使用AVAudioPlayer从服务器播放mp3。 I am trying to do it by using
我正在尝试通过使用
initWithData:(NSData *) error:(NSError **)
To do this I need to convert the mp3 to NSData then pass the NSData into my player. 为此,我需要将mp3转换为NSData,然后将NSData传递到播放器中。 How do I convert the mp3 to NSData.
如何将mp3转换为NSData。
NSData
has an - initWithContentsOfURL
: (or + dataWithContentsOfURL
:) method you could use. NSData
有一个initWithContentsOfURL
:(或+ dataWithContentsOfURL
:)方法可以使用。
Or, you could use NSURLConnection
to download your MP3 as NSData
. 或者,您可以使用
NSURLConnection
将您的MP3下载为NSData
。
I'd advise the second method, because it can be used asynchronously and doesn't block the main thread, unlike NSData
's - initWithContentsOfURL
: 我建议第二种方法,因为它可以异步使用并且不会阻塞主线程,这与
NSData
的initWithContentsOfURL
:
When you download it from the server, conveniently, you already have an NSData
object. 从服务器下载它时,很方便,您已经有一个
NSData
对象。 Did you try starting the player with that? 您是否尝试以此启动播放器?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.