仿钢琴的声音_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 仿钢琴的声音

仿钢琴的声音

 2014/10/22 3:13:44  粉粉色  程序员俱乐部  我要评论(0)
  • 摘要:效果图:当点击按钮的时候,会播放相应的钢琴的声音。此代码需要引入库AudioToolbox.framework.此为工程目录:RootViewController.h#import<UIKit/UIKit.h>//加入头文件#import<AudioToolbox/AudioToolbox.h>@interfaceRootViewController:UIViewController{NSString*soundFile;}@endRootViewController
  • 标签:

 

效果图:

当点击按钮的时候,会播放相应的钢琴的声音。

此代码需要引入库AudioToolbox.framework.

此为工程目录:

RootViewController.h

#import <UIKit/UIKit.h>
//加入头文件
#import <AudioToolbox/AudioToolbox.h>


@interface RootViewController : UIViewController
{
    NSString *soundFile;
}

@end

 

RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSArray *titleArray=[[NSArray alloc]initWithObjects:@"DO",@"RE",@"MI",@"FA",@"SO",@"LA",@"SI",@"C",@"D",@"E",@"F",@"G", nil];
    
    for (int i=0; i<12; i++) {
        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(50, 80+30*i, 70, 20)];
        [button setTitle:[titleArray objectAtIndex:i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(doClickAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag=i;
        button.backgroundColor=[UIColor redColor];
        [self.view addSubview:button];
    }
    
}
#pragma -mark -fucntions
-(void)playSound:(NSString*)soundKey{
    
    NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],soundKey];
    SystemSoundID soundID;
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
    AudioServicesPlaySystemSound(soundID);
    
}
#pragma -makr -doClickActions
-(void)doClickAction:(UIButton *)btn
{
    NSLog(@"---doClickActions--");
    NSLog(@"--btn.tag---%i",btn.tag);
    
    if (btn.tag==0) {
        soundFile = [NSString stringWithFormat:@"/001.mp3"];
        [self playSound: soundFile];

    }else if (btn.tag==1){
        soundFile = [NSString stringWithFormat:@"/002.mp3"];
        [self playSound: soundFile];
    }else if (btn.tag==2){
        soundFile = [NSString stringWithFormat:@"/003.mp3"];
        [self playSound: soundFile];
    }else if (btn.tag==3){
        soundFile = [NSString stringWithFormat:@"/004.mp3"];
        [self playSound: soundFile];
    }else if (btn.tag==4){
        soundFile = [NSString stringWithFormat:@"/005.mp3"];
        [self playSound: soundFile];
    }else if (btn.tag==5){
        soundFile = [NSString stringWithFormat:@"/006.mp3"];
        [self playSound: soundFile];
    }else if (btn.tag==6){
        soundFile = [NSString stringWithFormat:@"/007.mp3"];
        [self playSound: soundFile];
    }else if (btn.tag==7){
        soundFile = [NSString stringWithFormat:@"/C.mp3"];
        [self playSound: soundFile];
    }else if (btn.tag==8){
        soundFile = [NSString stringWithFormat:@"/D.mp3"];
        [self playSound: soundFile];

    }else if (btn.tag==9){
        soundFile = [NSString stringWithFormat:@"/E.mp3"];
        [self playSound: soundFile];

    }else if (btn.tag==10){
        soundFile = [NSString stringWithFormat:@"/F.mp3"];
        [self playSound: soundFile];

    }else if (btn.tag==11){
        soundFile = [NSString stringWithFormat:@"/G.mp3"];
        [self playSound: soundFile];

    }
}

 

  • 相关文章
发表评论
用户名: 匿名