Very short snippet: streaming mp3 file su Apple iPhone

martedì 1 dicembre, 2009

Un'alternativa davvero semplice per eseguire uno streaming di un file mp3 su Apple iPhone potrebbe essere:

Objective-C:
  1. // Utilizzare AVFoundation Framework e nell'header file (.h) aggiungere
  2. // #import <AVFoundation/AVFoundation.h>
  3.  
  4. // Nell'implementazione
  5. // inserite l'url del file mp3
  6. NSString * soundFilePath = @"http://www.server.com/test.mp3";
  7.  
  8. NSURL * fileURL = [NSURL URLWithString:soundFilePath]
  9. NSURLRequest * request = [NSURLRequest requestWithURL:fileURL];
  10. NSData *dataRaw = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  11.  
  12. AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithData:dataRaw error:nil];
  13.  
  14. [audioPlayer play];

Ora, non direttamente correlato a questo "snippet", guardate come è facile con la (tanto odiata) sintassi a parentesi quadre di Objective-c ridurre tutto ad una sola riga:

Objective-C:
  1. AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithData: [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL: [NSURL URLWithString: @"http://www.server.com/test.mp3" ] ] returningResponse:nil error:nil] error:nil];
  2. [audioPlayer play];

Indentando il codice un po' meglio si ha:

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

Questione di gusti, ovviamente...

Post correlati

Questo articolo ti è stato utile?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

Non ci sono commenti per questo Post

Lascia un commento

TAG XHTML PERMESSI: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERIMENTO CODICE:
<pre></pre>         // blocco generico
[code][/code]       // blocco generico
[as][/as]           // Actionscript
[css][/css]         // CSS Style Sheet
[html][/html]       // HTML
[js][/js]           // Javascript
[objc][/objc]       // Objective-C
[php][/php]         // PHP
[sql][/sql]         // SQL