2014年2月25日火曜日

iphoneアプリ開発 音楽を扱う方法

こんにちは、昨日はソフトバンクの電波状態が悪いせいか、
ブログの更新ができませんでした。
基本的には夜に更新していきますので、よろしくお願いいたします。

さて、本日は音源を扱う方法を紹介します。

まず扱いたいMP3ファイルをサポートファイルにドラッグします。




















⬅この辺に入れました。


このMP3ファイルを認識させるために、
ViewDidloadにプレイヤーオブジェクトの設定をします。
今回はスタートボタンとストップボタンを押下したときの効果音を流したいので
2つずつ記述しています。


- (void)viewDidLoad
{
    [super viewDidLoad];
    count = 0;
// Do any additional setup after loading the view, typically from a nib.
    
    //スタートボタン用mp3ファイル名設定
    NSString *soundFilePath =
    [[NSBundle mainBundle] pathForResource: @"se_maoudamashii_onepoint09"
                                    ofType: @"mp3"];
    //ストップボタン用mp3ファイル名設定
    NSString *soundFilePath2 =
    [[NSBundle mainBundle] pathForResource: @"se_maoudamashii_onepoint04"
                                    ofType: @"mp3"];
   
    NSURL *fileURL =
    [[NSURL alloc] initFileURLWithPath: soundFilePath];
    NSURL *fileURL2 =
    [[NSURL alloc] initFileURLWithPath: soundFilePath2];
   
    //プレイヤー準備
    AVAudioPlayer *newPlayer =
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                           error: nil];
    AVAudioPlayer *newPlayer2 =
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL2
                                           error: nil];
    self.player = newPlayer;
    self.player2 = newPlayer2;
    
    [player prepareToPlay];

    [player2 prepareToPlay];

これで準備完了です。
あとはスタートボタンとストップボタンの処理です。

//ストップボタンの処理
- (IBAction)startbtn2:(id) sender {
   //音楽再生

    [player2 play];
}

//スタートボタンの処理
- (IBAction)startbtn:(id) sender {
    //音楽再生

    [player play];
}

これで音楽をながせるはずです。

次回はビンゴ抽選時のランダムに数字を表示させるロジックを紹介したいと思います。








0 件のコメント:

コメントを投稿