わたしのための逆引きobjective-c

ブログ名のとおり、わたしの勉強したことをまとめていますので、他の方は分かりづらいかもしれません

ピアノのサンプルコード

f:id:chumix:20131024101247p:plain

 

 ①AVFoundation.frameworkを入れる

 

②出したい音のファイルをドラッグして入れておく

 

ViewController.hに記述

 

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

 

@interface ViewController : UIViewController

{

    

    AVAudioPlayer *_player[13];

 

}

 

@end

 

 

ViewController.mに記述

 

#import "ViewController.h"

 

#define BTN_DO 0

#define BTN_RE 1

#define BTN_MI 2

#define BTN_FA 3

#define BTN_SO 4

#define BTN_RA 5

#define BTN_SHI 6

#define BTN_DO_2 7

#define BTN_DO_1 8

#define BTN_RE_2 9

#define BTN_FA_3 10

#define BTN_SO_4 11

#define BTN_RA_5 12

 

 

@interfaceViewController ()

 

@end

 

@implementation ViewController

 

//テキストボタンの生成

- (UIButton*)makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag

{

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [button setTitle:text forState:UIControlStateNormal];

    [button setFrame:rect];

    [button setTag:tag];

    [button addTarget:selfaction:@selector(clickButton:) forControlEvents:UIControlEventTouchDown];

    return button;

    

}

 

//テキストボタンの生成

- (UIButton*)makeButton2:(CGRect)rect text:(NSString*)text tag:(int)tag

{

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [button setTitle:text forState:UIControlStateNormal];

    [button setFrame:rect];

    [button setTag:tag];

    [button addTarget:selfaction:@selector(clickButton:) forControlEvents:UIControlEventTouchDown];

    return button;

    

}

 

//オーディオプレーヤーの再生

- (AVAudioPlayer*)makeAudioPlayer:(NSString*)res

{

    

    //リソースのURLの再生

    NSString *path = [[NSBundlemainBundle] pathForResource:res ofType:@""];

    NSURL *url = [NSURL fileURLWithPath:path];

    

    //オーディオプレーヤーの生成

    return [[[AVAudioPlayeralloc] initWithContentsOfURL:url error:NULL] autorelease];

    

}

 

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    //ド

    UIButton *btn_do = [self makeButton:CGRectMake(0,45,60,240) text:@"ド" tag:BTN_DO];

    [self.view addSubview:btn_do];

    

    //レ

    UIButton *btn_re = [self makeButton:CGRectMake(60,45,60,240) text:@"レ" tag:BTN_RE];

    [self.view addSubview:btn_re];

    

    //ミ

    UIButton *btn_mi = [self makeButton:CGRectMake(120,45,60,240) text:@"ミ" tag:BTN_MI];

    [self.view addSubview:btn_mi];

    

    //ファ

    UIButton *btn_fa = [self makeButton:CGRectMake(180,45,60,240) text:@"ファ" tag:BTN_FA];

    [self.view addSubview:btn_fa];

    

    //ソ

    UIButton *btn_so = [self makeButton:CGRectMake(240,45,60,240) text:@"ソ" tag:BTN_SO];

    [self.view addSubview:btn_so];

    

    //ラ

    UIButton *btn_ra = [self makeButton:CGRectMake(300,45,60,240) text:@"ラ" tag:BTN_RA];

    [self.view addSubview:btn_ra];

    

    //シ

    UIButton *btn_shi = [self makeButton:CGRectMake(360,45,60,240) text:@"シ" tag:BTN_SHI];

    [self.view addSubview:btn_shi];

    

    //ド2

    UIButton *btn_do2 = [self makeButton:CGRectMake(420,45,60,240) text:@"ド" tag:BTN_DO_2];

    [self.view addSubview:btn_do2];

    

    //ド#

    UIButton *btn_do_1 = [self makeButton2:CGRectMake(40,45,40,150) text:@"ド#" tag:BTN_DO_1];

    btn_do_1.backgroundColor = [UIColorblackColor];

    btn_do_1.titleLabel.textColor = [UIColorwhiteColor];

    btn_do_1.showsTouchWhenHighlighted  = YES;//押した感じをだす 白丸がでる

    btn_do_1.titleLabel.font = [UIFont systemFontOfSize:14];

    [self.view addSubview:btn_do_1];

    

    //レ#

    UIButton *btn_re_2 = [self makeButton2:CGRectMake(100,45,40,150) text:@"レ#" tag:BTN_RE_2];

    btn_re_2.backgroundColor = [UIColorblackColor];

    btn_re_2.titleLabel.textColor = [UIColorwhiteColor];

    btn_re_2.showsTouchWhenHighlighted  = YES;

    btn_re_2.titleLabel.font = [UIFont systemFontOfSize:14];

    [self.view addSubview:btn_re_2];

    

    //ファ#

    UIButton *btn_fa_3 = [self makeButton2:CGRectMake(220,45,40,150) text:@"ファ#" tag:BTN_FA_3];

    btn_fa_3.backgroundColor = [UIColorblackColor];

    btn_fa_3.titleLabel.textColor = [UIColorwhiteColor];

    btn_fa_3.showsTouchWhenHighlighted  = YES;

    btn_fa_3.titleLabel.font = [UIFont systemFontOfSize:14];

    [self.view addSubview:btn_fa_3];

    

    //ソ#

    UIButton *btn_so_4 = [self makeButton2:CGRectMake(280,45,40,150) text:@"ソ#" tag:BTN_SO_4];

    btn_so_4.backgroundColor = [UIColorblackColor];

    btn_so_4.titleLabel.textColor = [UIColorwhiteColor];

    btn_so_4.showsTouchWhenHighlighted  = YES;

    btn_so_4.titleLabel.font = [UIFont systemFontOfSize:14];

    [self.view addSubview:btn_so_4];

    

    //ラ#

    UIButton *btn_ra_5 = [self makeButton2:CGRectMake(340,45,40,150) text:@"ラ#" tag:BTN_RA_5];

    btn_ra_5.backgroundColor = [UIColorblackColor];

    btn_ra_5.titleLabel.textColor = [UIColorwhiteColor];

    btn_ra_5.showsTouchWhenHighlighted  = YES;

    btn_ra_5.titleLabel.font = [UIFont systemFontOfSize:14];

    [self.view addSubview:btn_ra_5];

    

    UIView *blackView = [[[UIViewalloc] initWithFrame:CGRectMake(0,0,500,50)] autorelease];

    blackView.backgroundColor = [UIColorblackColor];

    [self.view addSubview:blackView];

    

    //プレーヤーの生成

    _player[0] = [[self makeAudioPlayer:@"do.wav"] retain];

    _player[1] = [[self makeAudioPlayer:@"re.wav"] retain];

    _player[2] = [[self makeAudioPlayer:@"mi.wav"] retain];

    _player[3] = [[self makeAudioPlayer:@"fa.wav"] retain];

    _player[4] = [[self makeAudioPlayer:@"so.wav"] retain];

    _player[5] = [[self makeAudioPlayer:@"ra.wav"] retain];

    _player[6] = [[self makeAudioPlayer:@"si.wav"] retain];

    _player[7] = [[self makeAudioPlayer:@"do2.wav"] retain];

    _player[8] = [[self makeAudioPlayer:@"9do#.m4a"] retain];

    _player[9] = [[self makeAudioPlayer:@"10re#.m4a"] retain];

    _player[10] = [[self makeAudioPlayer:@"11fa#.m4a"] retain];

    _player[11] = [[self makeAudioPlayer:@"12so#.m4a"] retain];

    _player[12] = [[self makeAudioPlayer:@"13ra#.m4a"] retain];

 

 

}

 

//メモリ解放

- (void)dealloc

{

    

    [_player[0] release];

    [_player[1] release];

    [_player[2] release];

    [_player[3] release];

    [_player[4] release];

    [_player[5] release];

    [_player[6] release];

    [_player[7] release];

    [_player[8] release];

    [_player[9] release];

    [_player[10] release];

    [_player[11] release];

    [_player[12] release];

 

 

    [super dealloc];

}

 

//ボタンクリック時のイベント処理

- (IBAction)clickButton:(UIButton*)sender

{

    

    if ( [sender tag] == BTN_DO ) {

        if (_player[0].playing) {

            _player[0].currentTime = 0;

        } else {

            [_player[0] play];

        }

    } else  if ( [sender tag] == BTN_RE ) {

        if (_player[1].playing) {

            _player[1].currentTime = 0;

        } else {

            [_player[1] play];

        }

    } else  if ( [sender tag] == BTN_MI ) {

        if (_player[2].playing) {

            _player[2].currentTime = 0;

        } else {

            [_player[2] play];

        }

    } else  if ( [sender tag] == BTN_FA ) {

        if (_player[3].playing) {

            _player[3].currentTime = 0;

        } else {

            [_player[3] play];

        }

    } else  if ( [sender tag] == BTN_SO ) {

        if (_player[4].playing) {

            _player[4].currentTime = 0;

        } else {

            [_player[4] play];

        }

    } else  if ( [sender tag] == BTN_RA ) {

        if (_player[5].playing) {

            _player[5].currentTime = 0;

        } else {

            [_player[5] play];

        }

    } else  if ( [sender tag] == BTN_SHI ) {

        if (_player[6].playing) {

            _player[6].currentTime = 0;

        } else {

            [_player[6] play];

        }

    } else  if ( [sender tag] == BTN_DO_2 ) {

        if (_player[7].playing) {

            _player[7].currentTime = 0;

        } else {

            [_player[7] play];

        }

    } else  if ( [sender tag] == BTN_DO_1 ) {

        if (_player[8].playing) {

            _player[8].currentTime = 0;

        } else {

            [_player[8] play];

        }

    } else  if ( [sender tag] == BTN_RE_2 ) {

        if (_player[9].playing) {

            _player[9].currentTime = 0;

        } else {

            [_player[9] play];

        }

    } else  if ( [sender tag] == BTN_FA_3 ) {

        if (_player[10].playing) {

            _player[10].currentTime = 0;

        } else {

            [_player[10] play];

        }

    } else  if ( [sender tag] == BTN_SO_4 ) {

        if (_player[11].playing) {

            _player[11].currentTime = 0;

        } else {

            [_player[11] play];

        }

    } else  if ( [sender tag] == BTN_RA_5 ) {

        if (_player[12].playing) {

            _player[12].currentTime = 0;

        } else {

            [_player[12] play];

        }

    }

 

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end