这一章的demo主要讲解如何运用音频文件服务,其中含有一个c语言编程接口,为程序开启音频类型的数据文件的读和写功能,把声音从存储器或缓存中读取或写入。
对于横向的屏幕设置有两种方法:一种就是在info.plist文件的Initial interface orientation关键字的value选择Landscape (left home button)。另一种方法就是在代码中更改:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
AudioToolbox框架,主要针对音频文件和音频流的回放和录音的服务,开启音频类型的数据文件的读和写功能,也支持管理音频文件以及播放系统的警告声音。在头文件引用如下:
#import <AudioToolbox/AudioToolbox.h>
//音频文件播放方法,soundKey是字符类型的音频文件名称
-(void)playSound:(NSString*)soundKey{
//获取音频文件的路径名称
NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],soundKey];
//释放字符类型变量对象
NSLog(@"%@\n", path);
//声明一个系统声音标识符
SystemSoundID soundID;
//转换字符类型变量的路径为URL链接,isDirectory:NO表明不存在目录
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
//使用AudioToolbox框架提供的创建声音服务,定义音频的路径和创建声音标识符
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//使用AudioToolbox框架提供的播放声音服务,播放指定的声音标识符
AudioServicesPlaySystemSound(soundID);
}
- (IBAction)DO:(id)sender{
//定义指定所要播放的音频文件名称
soundFile = [NSString stringWithFormat:@"/001.mp3"];
[self playSound: soundFile];
}
- (IBAction)RE:(id)sender{
soundFile = [NSString stringWithFormat:@"/002.mp3"];
[self playSound: soundFile];
}
- (IBAction)MI:(id)sender{
soundFile = [NSString stringWithFormat:@"/003.mp3"];
[self playSound: soundFile];
}
- (IBAction)FA:(id)sender{
soundFile = [NSString stringWithFormat:@"/004.mp3"];
[self playSound: soundFile];
}
- (IBAction)SO:(id)sender{
soundFile = [NSString stringWithFormat:@"/005.mp3"];
[self playSound: soundFile];
}
- (IBAction)LA:(id)sender{
soundFile = [NSString stringWithFormat:@"/006.mp3"];
[self playSound: soundFile];
}
- (IBAction)SI:(id)sender{
soundFile = [NSString stringWithFormat:@"/007.mp3"];
[self playSound: soundFile];
}
- (IBAction)C:(id)sender{
soundFile = [NSString stringWithFormat:@"/C.mp3"];
[self playSound: soundFile];
}
- (IBAction)D:(id)sender{
soundFile = [NSString stringWithFormat:@"/D.mp3"];
[self playSound: soundFile];
}
- (IBAction)E:(id)sender{
soundFile = [NSString stringWithFormat:@"/E.mp3"];
[self playSound: soundFile];
}
- (IBAction)F:(id)sender{
soundFile = [NSString stringWithFormat:@"/F.mp3"];
[self playSound: soundFile];
}
- (IBAction)G:(id)sender{
soundFile = [NSString stringWithFormat:@"/G.mp3"];
[self playSound: soundFile];
}