《30天精通iPhone手机编程》-Day19-电子琴

        这一章的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];
	
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值