Un’alternativa davvero semplice per eseguire uno streaming di un file mp3 su Apple iPhone potrebbe essere:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Utilizzare AVFoundation Framework e nell'header file (.h) aggiungere // #import <AVFoundation/AVFoundation.h> // Nell'implementazione // inserite l'url del file mp3 NSString * soundFilePath = @"http://www.server.com/test.mp3"; NSURL * fileURL = [NSURL URLWithString:soundFilePath]; NSURLRequest * request = [NSURLRequest requestWithURL:fileURL]; NSData *dataRaw = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithData:dataRaw error:nil]; [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:
1 2 | AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithData: [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL: [NSURL URLWithString: @"http://www.server.com/test.mp3" ] ] returningResponse:nil error:nil] error:nil]; [audioPlayer play]; |
Indentando il codice un po’ meglio si ha:
1 2 3 4 5 6 7 8 9 10 | AVAudioPlayer * audioPlayer = [ [AVAudioPlayer alloc] initWithData: [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL: [NSURL URLWithString: @"http://www.server.com/test.mp3" ] ] returningResponse:nil error:nil] error:nil]; [audioPlayer play]; |
Questione di gusti, ovviamente…









11
Non ci sono commenti per questo Post
Lascia un commento