2009-07-19 212 views
1

下面是代碼:使用系統聲音播放聲音

-(void)stop 
{ 
    NSLog(@"Disposing Sounds"); 
    AudioServicesDisposeSystemSoundID (soundID); 
    //AudioServicesRemoveSystemSoundCompletion (soundID); 
} 

static void completionCallback (SystemSoundID mySSID, void* myself) { 
    NSLog(@"completion Callback"); 
} 
- (void) playall: (id) sender { 

    [self stop]; 

    AudioServicesAddSystemSoundCompletion (soundID,NULL,NULL, 
    completionCallback, 
    (void*) self); 


    OSStatus err = kAudioServicesNoError; 
    NSString *aiffPath = [[NSBundle mainBundle] pathForResource:@"slide1" ofType:@"m4a"]; 
    NSURL *aiffURL = [NSURL fileURLWithPath:aiffPath]; 
    err = AudioServicesCreateSystemSoundID((CFURLRef) aiffURL, &soundID); 
    AudioServicesPlaySystemSound (soundID); 
    NSLog(@"Done Playing"); 
} 

輸出:

Disposing Sounds 
Done Playing 

在實際沒有聲音得到根本發揮和完成回叫不叫爲好。任何想法在這裏可能是錯的? 我想在播放當前之前停止任何以前的聲音。

回答

0

err包含什麼?從那你應該能夠推斷出這個問題。

+0

我打印ERR使用NSLog和apears(null) – Shoaibi 2009-07-19 01:37:31

1

據我所知,唯一支持文件的.caf,.AIF,或.wav:

To play your own sounds, add the sound file to your application bundle; the sound file must adhere to the following requirements:

  • Must be .caf, .aif, or .wav files.
  • The audio data in the file must be in PCM or IMA/ADPCM (IMA4) format.
  • The file's audio duration must be less than 30 seconds.

Audio & Video Coding How-To's

0

可以使用AudioToolBox框架是:

CFBundleRef mainbundle = CFBundleGetMainBundle(); 
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainbundle, CFSTR("tap"), CFSTR("aif"), NULL); 
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject); 
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 
AudioServicesPlaySystemSound(1100);