2012-03-20 10 views

Odpowiedz

25

Jest to najlepszy sposób grania prostego dźwięku w iOS (nie więcej niż 30 sekund):

//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

po prostu zrobił to sam. –

+0

dzięki! zadziałało! – noloman

+0

Dzięki bro !! jego prace .. –

29

Używam tego:

plik nagłówka:

#import <AudioToolbox/AudioServices.h> 

@interface SoundEffect : NSObject 
{ 
    SystemSoundID soundID; 
} 

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

@end 

Plik źródłowy:

#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 

Będziesz musiał stworzyć instancję SoundEffect i wywołać metodę metody.

+0

To jest świetne. Używałem tablic C z SystemSoundID, ale właśnie uderzyłem w punkt, w którym było to zbyt trudne do pokonania. Przejście do czegoś na podstawie tego. Dzięki! –

+2

To nie działa z ARC. Aby użyć go z ARC, musisz dodać funkcję wywołania zwrotnego zakończenia, gdzie usuniesz systemound. Jeśli zrobisz to w dealloc, dźwięk zostanie natychmiast zabity: 'AudioServicesAddSystemSoundCompletion (soundID, NULL, NULL, completionCallback, (__bridge_retained void *) self);' Podobnie jak w przykładzie – Maverick1st

+0

@ Maverick1st Działa to doskonale z ARC, po prostu musisz upewnić się, że obiekt 'SoundEffect' nie zostanie natychmiast zwolniony, na przykład przez przypisanie go do właściwości. – shawkinaw

10

(Mała poprawka do prawidłowej odpowiedzi, aby dbać o utylizacji dźwiękiem)

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

To jest najlepszy sposób odtwarzania dźwięku asynchronicznie. – alones

Powiązane problemy