Very short snippet: streaming mp3 files to Apple iPhone

An alternative really easy to run a stream a mp3 file on the Apple iPhone could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ / Use AVFoundation Framework and header files (. H) add
/ / # Import <AVFoundation/AVFoundation.h>

/ / In the implementation
/ / Put the url of the mp3 file
soundFilePath = @ "http://www.server.com/test.mp3" ; NSString * soundFilePath @ = "http://www.server.com/test.mp3";

fileURL = [ NSURL URLWithString : soundFilePath ] ; NSURL * fileURL = [ NSURL URLWithString: soundFilePath];
request = [ NSURLRequest requestWithURL : fileURL ] ; NSURLRequest * request = [ NSURLRequest requestWithURL: fileURL];
dataRaw = [ NSURLConnection sendSynchronousRequest : request returningResponse : nil error : nil ] ; NSData * dataRaw = [ NSURLConnection sendSynchronousRequest: returningResponse request: nil error: nil];

[ [ AVAudioPlayer alloc ] initWithData : dataRaw error : nil ] ; AVAudioPlayer * AudioPlayer = [[AVAudioPlayer alloc] initWithData: dataRaw error: nil];

; [AudioPlayer play];

Now, not directly related to this "snippet", look how easy it is with the (hated) in square brackets syntax of Objective-C to reduce everything to a single line:

1
2
[ [ AVAudioPlayer AVAudioPlayer * AudioPlayer = [[AVAudioPlayer
; [AudioPlayer play];

Indenting the code a bit 'better, we have:

1
2
3
4
5
6
7
8
9
10
AVAudioPlayer * AudioPlayer =
[
[AVAudioPlayer alloc]
NSURLConnection initWithData: [ NSURLConnection
NSURLRequest sendSynchronousRequest: [ NSURLRequest
NSURL URLWithString : @ "http://www.server.com/test.mp3" ] requestWithURL: [ NSURL URLWithString: @ "http://www.server.com/test.mp3"]
]
error : nil ] returningResponse: nil error: nil]
] ; error: nil];
; [AudioPlayer play];

Matter of taste, of course ...

There are no comments for this post

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA