IOS基础:声音调用
来源:互联网 发布:友情卡片 知乎 编辑:程序博客网 时间:2024/04/28 14:48
AudioToolbox framework
使用AudioToolbox framework。这个框架可以将比较短的声音注册到 system sound服务上。被注册到system sound服务上的声音称之为 system sounds。它必须满足下面几个条件。
1、 播放的时间不能超过30秒
2、数据必须是 PCM或者IMA4流格式
3、必须被打包成下面三个格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff)
声音文件必须放到设备的本地文件夹下面。通过AudioServicesCreateSystemSoundID方法注册这个声音文件,AudioServicesCreateSystemSoundID需要声音文件的url的CFURLRef对象。看下面注册代码:
#import <AudioToolbox/AudioToolbox.h>
-(void) playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource@"beep" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&sound);
AudioServicesPlaySystemSound(soundID);
}
这样就可以使用下面代码播放声音了
使用下面代码,还加一个震动的效果:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AVFoundation framework
对于压缩过Audio文件,或者超过30秒的音频文件,可以使用AVAudioPlayer类。这个类定义在AVFoundation framework中
#import <AVFoundation/AVFoundation.h>
@interface MediaPlayerViewController : UIViewController <AVAudioPlayerDelegate>{
IBOutlet UIButton *audioButton;
SystemSoundID shortSound;
AVAudioPlayer *audioPlayer;
}
{
NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Music" ofType:@"mp3"];
if (musicPath) {
NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
[audioPlayer setDelegate:self];
}
我们可以在一个button的点击事件中开始播放这个mp3文件,如:
- (IBAction)playAudioFile:(id)sender{
if ([audioPlayer isPlaying]) {
// Stop playing audio and change text of button
[audioPlayer stop];
[sender setTitle:@"Play Audio File" forState:UIControlStateNormal];
} else {
// Start playing audio and change text of button so
// user can tap to stop playback
[audioPlayer play];
[sender setTitle:@"Stop Audio File"
forState:UIControlStateNormal];
}
}
这样运行我们的程序,就可以播放音乐了。
这个类对应的AVAudioPlayerDelegate有两个委托方法。一个是 audioPlayerDidFinishPlaying:successfully: 当音频播放完成之后触发。当播放完成之后,可以将播放按钮的文本重新回设置成:Play Audio File
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[audioButton setTitle:@"Play Audio File" forState:UIControlStateNormal];
}
另一个是audioPlayerEndInterruption:,当程序被应用外部打断之后,重新回到应用程序的时候触发。在这里当回到此应用程序的时候,继续播放音乐。
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player{ [audioPlayer play];}
我们在听音乐的时候,可以用iphone做其他的事情,这个时候需要播放器在后台也能运行,我们只需要在应用程序中做个简单的设置就行了。
1、在Info property list中加一个 Required background modes节点,它是一个数组,将第一项设置成设置App plays audio。
2、在播放mp3的代码中加入下面代码:
if (musicPath) {
NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
[audioPlayer setDelegate:self];
}
在后台运行的播放音乐的功能在模拟器中看不出来,只有在真机上看效果。
- IOS基础:声音调用
- IOS基础:声音调用
- IOS基础:声音调用
- IOS声音调用
- iOS调用系统声音
- iOS-系统声音列表及调用
- IOS调用系统震动和系统声音
- iOS调用系统声音和振动
- iOS调用系统声音与振动
- IOS基础--异步调用
- ios 设置亮度、声音;调用发短信、邮件、打电话
- iOS学习笔记(1)——声音调用
- 【代码笔记】iOS-调用系统震动和声音
- iOS 接收新消息通知调用系统声音 震动
- IOS-播放声音,中断声音
- ios20-调用系统声音
- android 声音的调用
- 调用系统声音
- 高度自适应
- UVa 401 Palindromes (回文串与镜像串的综合判断)
- 【bzoj 3110】[Zjoi2013]K大数查询|树套树
- JavaScript中的关键字之prototype的使用
- struts2+Hibernate实现用户登陆功能
- IOS基础:声音调用
- nginx配置vhost例子
- iOS访UC、腾讯视频播放
- 【html5和css3】animation属性整理
- Singleton 单例模式
- 微信Android客户端架构演进之路
- gearman安装
- mysql的基本查询语句
- iOS用xib文件创建UITableView