Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this questionI'm trying to implement a progressive download audio player for the iPhone, using http and fixed size mp3-files.
开发者_运维技巧I found the AudioStreamer project but it seems very complicated and works best with endless streams.
I need to be able to find out the total length of audio files and I also need to be able to seek in the files. I found a hacked deviation from AudioStreamer but it doesn't seem to work very well for me. http://www.saygoodnight.com/?p=14
I'm wondering if there is a simpler way to achieve my goals or if there are some better working samples out there? I found the bass library but not much documentation about it.
/Br Johannes
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
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
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
精彩评论