NSSound sur l'iphone
-
23-08-2019 - |
Question
Je l'ai cherché pendant un certain temps à jouer son sur l'iPhone, et je pense que c'est quelque chose le long des lignes de:
[[NSSound soundNamed:@"cat.mp3"] play];
Mais le NSSound est sur le AppKit ... des suggestions? Je sais qu'il ya une réponse très simple à cela, mais aujourd'hui mes recherches ne fournissent aucun aucun résultat ...
La solution
la nouvelle classe AVFoundation est la meilleure façon de jouer son sur l'iPhone, mais il est pour le firmware 2.2 seulement:
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[audioPlayer play];
vous avez juste besoin cette importation dans la classe qui l'utilise:
#import <AVFoundation/AVAudioPlayer.h>
Autres conseils
sons système Boîte à outils audio sont parfaits pour la lecture à court async.
// Initialize
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
// Init each sound
CFURLRef tapURL;
SystemSoundID tapSound;
tapURL = CFBundleCopyResourceURL(mainBundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(tapURL, &tapSound);
// Play the sound async
AudioServicesPlaySystemSound(tapSound);
Je ne l'ai pas obtenu des fichiers MP3 à travailler avec AudioServicesPlaySystemSound
. Mais il joue des fichiers AIF parfaitement.