▼iPhoneプログラミングメモ▼
サウンドの再生

サウンドを再生するプログラムを作成する。


フレームワークの準備
AVFoundationフレームワークを追加。

リソースの準備
以下のサウンドファイルをプロジェクトのResourcesにドラッグ&ドロップで追加。
wavは同時再生可。mp3は不可。 ソースコードの記述
AudioEx.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

//AudioExの宣言
@interface AudioEx : UIViewController {
    AVAudioPlayer* _player[2];
}
@end

AudioEx.m
#import "AudioEx.h"

#define BTN_BGM_PLAY 0
#define BTN_SE_PLAY  1
#define BTN_VOL_UP   2
#define BTN_VOL_DOWN 3

//AudioExの実装
@implementation AudioEx

//テキストボタンの生成
- (UIButton*)makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag {
    UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:text forState:UIControlStateNormal];
    [button setFrame:rect];
    [button setTag:tag];
    [button addTarget:self action:@selector(clickButton:) 
        forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//オーディオプレーヤーの生成
- (AVAudioPlayer*)makeAudioPlayer:(NSString*)res {
    //リソースのURLの生成
    NSString* path=[[NSBundle mainBundle] pathForResource:res ofType:@""];
    NSURL* url=[NSURL fileURLWithPath:path];
    
    //オーディオプレーヤーの生成
    return [[[AVAudioPlayer alloc] initWithContentsOfURL:url 
        error:NULL] autorelease];
}

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];
        
    //BGM再生ボタンの生成
    UIButton* btnBGMStart=[self makeButton:CGRectMake(0,0,200,40) 
        text:@"BGM再生/停止" tag:BTN_BGM_PLAY];
    [self.view addSubview:btnBGMStart];
        
    //SE再生ボタンの生成
    UIButton* btnSEPlay=[self makeButton:CGRectMake(0,50,200,40) 
        text:@"SE再生" tag:BTN_SE_PLAY];
    [self.view addSubview:btnSEPlay];

    //ボリュームアップボタンの生成
    UIButton* btnVolUp=[self makeButton:CGRectMake(0,100,200,40) 
        text:@"ボリュームアップ" tag:BTN_VOL_UP];
    [self.view addSubview:btnVolUp];
        
    //ボリュームダウンボタンの生成
    UIButton* btnVolDown=[self makeButton:CGRectMake(0,150,200,40) 
        text:@"ボリュームダウン" tag:BTN_VOL_DOWN];
    [self.view addSubview:btnVolDown];
        
    //プレーヤーの生成
    _player[0]=[[self makeAudioPlayer:@"bgm.wav"] retain];
    _player[1]=[[self makeAudioPlayer:@"se.wav"] retain];
}

//メモリ解放
- (void)dealloc {
    [_player[0] release];
    [_player[1] release];
    [super dealloc];
}

//ボタンクリック時のイベント処理
- (IBAction)clickButton:(UIButton*)sender {
    if ([sender tag]==BTN_BGM_PLAY) {
        //BGMの再生と停止
        if (!_player[0].playing) {
            _player[0].numberOfLoops=999;
            _player[0].currentTime=0;
            [_player[0] play]; 
        } else {
            [_player[0] stop];        
        }      
    } else if ([sender tag]==BTN_SE_PLAY) {
        //SEの再生
        if (_player[1].playing) {
            _player[1].currentTime=0;
        } else {
            [_player[1] play];        
        }
    } else if ([sender tag]==BTN_VOL_UP) {
        //オーディオプレーヤーのボリューム操作
        float volume=_player[0].volume+0.2f;
        if (volume>1.0f) volume=1.0f;
        _player[0].volume=volume;        
        _player[1].volume=volume;        
    } else if ([sender tag]==BTN_VOL_DOWN) {
        //オーディオプレーヤーのボリューム操作
        float volume=_player[0].volume-0.2f;
        if (volume<0.0f) volume=0.0f;
        _player[0].volume=volume;        
        _player[1].volume=volume;        
    }
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end



−戻る−