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