I know this is a rather old post, but wanted to suggest a solid library that is open source. It is based on Matt Gallagher's original post recommended by @pzearfoss above. The current version uses AudioUnits now, rather than the AudioQueue classes, so it gives you access to the raw PCM samples for any manipulation you want to make (filtering, etc.) before playback. It also gives you progressive download capabilities for free, with rather minimal effort. The library seems to be actively updated which is a huge plus!
There's unfortunately nothing simple about playing audio streams on the iPhone. Here's the article that got me started: Streaming and playing an MP3 stream
It's an OSX project, but most of it will work with the iPhone too. As for getting the full play time of it, you'd probably have to figure that out based on the content-length property of http header, provided it's a CBR file. Otherwise, I imagine you'd have to download the entire file before determining that.
If you only need to play MP3 files, why are you writing your own downloader/player? You have a few options built into iOS, each of which support progressive download and MP3 playback:
MPMoviePlayerController
AVPlayer
Safari <audio> player
I've personally had issues with the progressive download capabilities of MPMoviePlayerController and AVPlayer, so perhaps this is your issue also. I've found that these players try to be smart by requesting the mp3 multiple times, checking to see if the server supports progressive download via http range offsets. But when the server doesn't support range offsets the file is downloaded multiple times, eating bandwidth (!).
In my latest project I embedded a UIWebView having a html <audio> tag embedded. The Safari player seems to behave better than AVPlayer and MPMoviePlayerController, but it had its own caveats as well. For one, getting autoplay to work was a PITA.
There's a really good chapter about streaming audio in iPhone Cool Projects. http://apress.com/book/view/9781430223573 It shows a simpler approach then AudioStreamer (Using NSURLConnection instead of CFNetwork), better suited for progressive downloading, and no multi threading code.
And for finding out the size of the audio file you can use [response expectedContentLength] in
发布评论
评论(4)
我知道这是一篇相当老的文章,但想建议一个可靠的开源库。它基于上面 @pzearfoss 推荐的 Matt Gallagher 的原始帖子。当前版本现在使用 AudioUnits,而不是 AudioQueue 类,因此它使您可以访问原始 PCM 样本,以便在播放之前进行任何操作(过滤等)。它还为您提供免费的渐进式下载功能,只需花费很少的精力。该库似乎正在积极更新,这是一个巨大的优势!
StreamingKit 作者:@tumtumtum
I know this is a rather old post, but wanted to suggest a solid library that is open source. It is based on Matt Gallagher's original post recommended by @pzearfoss above. The current version uses AudioUnits now, rather than the AudioQueue classes, so it gives you access to the raw PCM samples for any manipulation you want to make (filtering, etc.) before playback. It also gives you progressive download capabilities for free, with rather minimal effort. The library seems to be actively updated which is a huge plus!
StreamingKit by @tumtumtum
不幸的是,在 iPhone 上播放音频流并不简单。这是让我开始的文章:
流式传输和播放 MP3 流
这是一个 OSX 项目,但其中大部分也适用于 iPhone。至于获得它的完整播放时间,您可能必须根据 http 标头的内容长度属性来计算,前提是它是 CBR 文件。否则,我想您必须先下载整个文件才能确定。
There's unfortunately nothing simple about playing audio streams on the iPhone. Here's the article that got me started:
Streaming and playing an MP3 stream
It's an OSX project, but most of it will work with the iPhone too. As for getting the full play time of it, you'd probably have to figure that out based on the content-length property of http header, provided it's a CBR file. Otherwise, I imagine you'd have to download the entire file before determining that.
如果您只需要播放 MP3 文件,为什么要编写自己的下载器/播放器? iOS 中内置了一些选项,每个选项都支持渐进式下载和 MP3 播放:
MPMoviePlayerController
AVPlayer
播放器
I'我个人对
MPMoviePlayerController
和AVPlayer
的渐进式下载功能有疑问,所以也许这也是您的问题。我发现这些播放器试图通过多次请求 mp3 来聪明地检查服务器是否支持通过 http 范围偏移进行渐进式下载。但是,当服务器不支持范围偏移时,文件会被多次下载,从而消耗带宽(!)。在我最新的项目中,我嵌入了一个嵌入了 html
标签的 UIWebView。 Safari 播放器的表现似乎比 AVPlayer 和 MPMoviePlayerController 更好,但它也有自己的注意事项。首先,让自动播放发挥作用是一项 PITA。
If you only need to play MP3 files, why are you writing your own downloader/player? You have a few options built into iOS, each of which support progressive download and MP3 playback:
MPMoviePlayerController
AVPlayer
<audio>
playerI've personally had issues with the progressive download capabilities of
MPMoviePlayerController
andAVPlayer
, so perhaps this is your issue also. I've found that these players try to be smart by requesting the mp3 multiple times, checking to see if the server supports progressive download via http range offsets. But when the server doesn't support range offsets the file is downloaded multiple times, eating bandwidth (!).In my latest project I embedded a UIWebView having a html
<audio>
tag embedded. The Safari player seems to behave better than AVPlayer and MPMoviePlayerController, but it had its own caveats as well. For one, getting autoplay to work was a PITA.iPhone Cool Projects 中有一个关于流音频的非常好的章节。
http://apress.com/book/view/9781430223573
它展示了一种比 AudioStreamer 更简单的方法(使用 NSURLConnection 而不是 CFNetwork),更适合渐进式下载,并且没有多线程代码。
要找出音频文件的大小,您可以使用
[response ExpectContentLength]
There's a really good chapter about streaming audio in iPhone Cool Projects.
http://apress.com/book/view/9781430223573
It shows a simpler approach then AudioStreamer (Using NSURLConnection instead of CFNetwork), better suited for progressive downloading, and no multi threading code.
And for finding out the size of the audio file you can use
[response expectedContentLength]
in