カエルの歌を歌うキーボードのサンプルを組んでみる。

(XcodeのiOS6 Simulatorで試しています。)

ポイント

 mp3ファイルの開始、終了の時間を設定する

 AVAudioPlayer setCurrentTimeで開始時間

 performSelectorで指定時間後に停止タイマー

サンプルコード(全部 ViewController.m の中に記述)

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

#import <AVFoundation/AVFoundation.h>

// カエルのView

@interface KaeruView : UIView {

    CGPoint upCenter;

}

@property (nonatomic, assign) float start;

@property (nonatomic, assign) float end;

– (void)open;

@end

@implementation KaeruView

@synthesize start, end;

– (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColor clearColor];

        self.clipsToBounds = YES;

        upCenter = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.6);

    }

    return self;

}

– (void)drawRect:(CGRect)rect

{

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 口を書く

    [[UIColor greenColor] setFill];

    CGContextAddArc(ctx, upCenter.x, upCenter.y, self.bounds.size.width * 0.4, 0, M_PI, YES);

    CGContextDrawPath(ctx, kCGPathFillStroke);

    

    CGPoint bottomCenter = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.59);

    CGContextAddArc(ctx, bottomCenter.x, bottomCenter.y, self.bounds.size.width * 0.4, 0, M_PI, NO);

    CGContextDrawPath(ctx, kCGPathFillStroke);

    

    //

    [[UIColor darkGrayColor] set];

    CGContextSetLineWidth(ctx, 5);

    float eyerx = upCenter.xself.bounds.size.width * 0.3;

    float eyery = upCenter.yself.bounds.size.height * 0.3;

    CGContextAddArc(ctx, eyerx, eyery, self.bounds.size.width * 0.1, 0, 2*M_PI, YES);

    CGContextDrawPath(ctx, kCGPathStroke);

    float eyelx = upCenter.x + self.bounds.size.width * 0.3;

    float eyely = upCenter.yself.bounds.size.height * 0.3;

    CGContextAddArc(ctx, eyelx, eyely, self.bounds.size.width * 0.1, 0, 2*M_PI, YES);

    CGContextDrawPath(ctx, kCGPathStroke);

}

– (void)open

{

    float original = upCenter.y;

    upCenter.y -= 5;

    [self setNeedsDisplay];

    

    float time = (endstart) * NSEC_PER_SEC;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, time), dispatch_get_main_queue(), ^{

        upCenter.y = original;

        [self setNeedsDisplay];

    });

}

@end

//

// ViewController

//

@interface ViewController () <AVAudioPlayerDelegate>

{

    AVAudioPlayer *player;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createKaeruBoard];

    [self setupKaerunoUta];

}

– (void)createKaeruBoard

{

    float soundDuration = 60.0 / 120.0;    

    for (int i=0; i<7; i++) {

        float x = 30 * i + 40;

        float y = 400 – i * 50;

        KaeruView *kaeruKA = [[KaeruView alloc] initWithFrame:CGRectMake(x, y, 50, 50)];

        kaeruKA.start = soundDuration * i;

        kaeruKA.end = soundDuration * (i+1);

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(kero:)];

        [kaeruKA addGestureRecognizer:tap];

        [self.view addSubview:kaeruKA];

    }

}

– (void)setupKaerunoUta

{

    NSString *path = [[NSBundle mainBundle] pathForResource:@”kaeru” ofType:@”mp3″];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];

    player.delegate = self;

}

– (void)pauseSound

{

    [player pause];

}

– (void)kero:(UIGestureRecognizer*)gr

{

    KaeruView *kv = (KaeruView*)gr.view;

    [kv open];

    

    [player setCurrentTime:kv.start];

    if([player prepareToPlay] && [player play]) {

        float duration = kv.end – kv.start0.1;

        [self performSelector:@selector(pauseSound) withObject:nil afterDelay:duration];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end