您的位置:首页 > 移动开发 > IOS开发

IOS开发:多媒体-音频的操作(一) 短声音的播放

2012-07-30 12:03 627 查看
一:基本知识
播放短声音主要有两个步骤:
(1)注册声音 方法:AudioServicesCreateSystemSoundID ((CFURLRef)fileURL,&myID);
(2)播放声音 方法:AudioServicesPlaySystemSound (myID);

监听完成事件方法
AudioServicesAddSystemSoundCompletion

清除播放sound ID(释放?)
AudioServicesAddSystemSoundCompletion
AudioServicesDisposeSystemSoundID (myID);
震动
可以通过System Sound APIiPhone震动,但是iPod touch不能震动。
震动可以通过指定一个特殊的system sound ID——kSystemSoundID_Vibrate实现。
方法:AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);

二:实例操作

完成短声音的播放实例,界面设计如下

运行xcode:新建项目名称为SystemSoundServices的single view application。

添加框架AudioToolbox.framework



打开ViewController.h 文件
添加头文件

打开ViewController.m 文件
添加方法 playSystemSound 和 vibrate
播放事件:

- (IBAction)playSystemSound:(id)sender
{
NSURL* system_sound_url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"BeepGMC500" ofType:@"wav"]];
SystemSoundID system_sound_id;
AudioServicesCreateSystemSoundID(
(__bridge CFURLRef)system_sound_url,
&system_sound_id
);
// Register the sound completion callback.
AudioServicesAddSystemSoundCompletion(
system_sound_id,
NULL, // uses the main run loop
NULL, // uses kCFRunLoopDefaultMode
MySoundFinishedPlayingCallback, // the name of our custom callback function
NULL // for user data, but we don't need to do that in this case, so we just pass NULL
);
// Play the System Sound
AudioServicesPlaySystemSound(system_sound_id);
}
震动事件:
- (IBAction)vibrate:(id)sender
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
再xib文件中完成方法的连接

回调函数实现:

void MySoundFinishedPlayingCallback(SystemSoundID sound_id, void* user_data)
{
AudioServicesDisposeSystemSoundID(sound_id);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: