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

一:基本知识

播放短声音主要有两个步骤:

(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);
}


















  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值