2012-03-20 12 views

risposta

25

Questo è il modo migliore per la riproduzione di un suono semplice in iOS (non più di 30 secondi):

//Retrieve audio file 
NSString *path = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"m4a"]; 
NSURL *pathURL = [NSURL fileURLWithPath : path]; 

SystemSoundID audioEffect; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect); 
AudioServicesPlaySystemSound(audioEffect); 

// call the following function when the sound is no longer used 
// (must be done AFTER the sound is done playing) 
AudioServicesDisposeSystemSoundID(audioEffect); 
+0

ho appena fatto io stesso. –

+0

grazie! ha funzionato! – noloman

+0

Grazie fratello !! i suoi lavori .. –

29

Io uso questo:

file di intestazione:

#import <AudioToolbox/AudioServices.h> 

@interface SoundEffect : NSObject 
{ 
    SystemSoundID soundID; 
} 

- (id)initWithSoundNamed:(NSString *)filename; 
- (void)play; 

@end 

File sorgente:

#import "SoundEffect.h" 

@implementation SoundEffect 

- (id)initWithSoundNamed:(NSString *)filename 
{ 
    if ((self = [super init])) 
    { 
     NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; 
     if (fileURL != nil) 
     { 
      SystemSoundID theSoundID; 
      OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID); 
      if (error == kAudioServicesNoError) 
       soundID = theSoundID; 
     } 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    AudioServicesDisposeSystemSoundID(soundID); 
} 

- (void)play 
{ 
    AudioServicesPlaySystemSound(soundID); 
} 

@end 

Sarà necessario creare un'istanza di SoundEffect e chiamare direttamente il metodo su di esso.

+0

Questo è fantastico. Stavo usando gli array C di SystemSoundIDs, ma ho appena raggiunto un punto in cui era troppo schlepp da affrontare. Passare a qualcosa in base a questo. Grazie! –

+2

Tuttavia, ciò non funziona con ARC. Per utilizzarlo con ARC è necessario aggiungere una funzione di callback di completamento, in cui si dispone del sistema. Se lo fai in dealloc il suono viene ucciso immediatamente: 'AudioServicesAddSystemSoundCompletion (soundID, NULL, NULL, completionCallback, (__bridge_retained void *) self);' Come questo per Esempio – Maverick1st

+0

@ Maverick1st Funziona perfettamente con ARC, hai solo assicurati che l'oggetto 'SoundEffect' non venga immediatamente deallocato, ad esempio assegnandolo a una proprietà. – shawkinaw

10

(Piccola modifica alla risposta corretta per prendersi cura della Smaltimento dell'audio)

NSString *path = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"m4a"]; 
NSURL *pathURL = [NSURL fileURLWithPath : path]; 

SystemSoundID audioEffect; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect); 
AudioServicesPlaySystemSound(audioEffect); 
// Using GCD, we can use a block to dispose of the audio effect without using a NSTimer or something else to figure out when it'll be finished playing. 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    AudioServicesDisposeSystemSoundID(audioEffect); 
}); 
+1

Questo è il modo migliore per riprodurre il suono in modo asincrono. – alones

Problemi correlati