Très court extrait: le streaming des fichiers MP3 sur l'iPhone d'Apple

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

XHTML TAG PERMIS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> CODE D'INSERTION:
 <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