Une alternative très simple à exécuter un flux d'un fichier mp3 sur l'iPhone d'Apple pourrait être:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | / Cadre AVFoundation / utilisation et les fichiers d'en-tête (. H) pour ajouter <AVFoundation/AVFoundation.h> / / # Import / / Dans la mise en œuvre / / Mettre l'URL du fichier mp3 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 demande * = [ NSURLRequest requestWithURL: fileURL]; dataRaw = [ NSURLConnection sendSynchronousRequest : request returningResponse : nil error : nil ] ; NSData * dataRaw = [ NSURLConnection sendSynchronousRequest: demande returningResponse: erreur nul: nil]; [ [ AVAudioPlayer alloc ] initWithData : dataRaw error : nil ] ; AVAudioPlayer * AudioPlayer = [[AVAudioPlayer alloc] initWithData: erreur dataRaw: nil]; ; [AudioPlayer jeu]; |
Maintenant, non directement liés à ce "snippet", regardez combien il est facile avec le (détesté) dans la syntaxe de crochets d'Objective-C à tout réduire à une seule ligne:
1 2 | [ [ AVAudioPlayer AVAudioPlayer * AudioPlayer = [[AVAudioPlayer ; [AudioPlayer jeu]; |
Indentation du code un peu "mieux, nous avons:
1 2 3 4 5 6 7 8 9 10 | * = AudioPlayer AVAudioPlayer [ [Alloc AVAudioPlayer] 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: erreur nul: nil] ] ; erreur: nil]; ; [AudioPlayer jeu]; |
Question de goût, bien sûr ...











Il n'y a aucun commentaire pour cet article
Laisser un commentaire