iOS播放鈴聲或者設置震動實現:
鈴聲: SystemSoundID soundID = 1007; AudioServicesPlaySystemSound(soundID); 振動: AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
其中,鈴聲的soundID是對應系統里鈴聲的ID,如何查看,點擊:http://iphonedevwiki.net/index.php/AudioServices 可進行查看
如下:
以上圖表是對應的系統級的鈴聲列表,格式為.caf
當然,我們也可以自己定義自己的鈴聲,格式包括:.caf .wav(其他格式暫未測試)
實現代碼如下:
- (void)playBeep { SystemSoundID sound = kSystemSoundID_Vibrate; //使用系統鈴聲 //NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",@"new-mail",@"caf"];
//使用自定義鈴聲 NSString *path = [[NSBundle mainBundle] pathForResource:@"QRCodeRead"ofType:@"wav"]; //需將音頻資源copy到項目
if (path) { OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&sound); if (error != kAudioServicesNoError) { sound = 0; } } AudioServicesPlaySystemSound(sound);//播放聲音 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//靜音模式下震動 }
綜上~