iPhone/iPadアプリ開発逆引き大全500の極意

プログラミングの極意 - Tips412 サウンドを開始する

登録タグ
iOS
Icon comment count 0
Icon stock count 0

5-14サウンドTips412 サウンドを開始する

Level1

Point音楽の再生(AVAudioPlayerクラス、initWithContentURL:メソッド、playメソッド)

音楽を再生する場合には、AVAudioPlayerクラスを使います。

initWithContentURL:メソッドで、再生する音楽ファイルを指定した後で、playメソッドで実行します。

リスト1では、リソースにある「music.mp3」ファイルを再生しています。

リスト1 音楽を再生する(ファイル名:prog412.xcodeproj)
-(IBAction)clickStart:(id)sender
{
    // 音楽を再生する
    NSString *path = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
    [audioPlayer play];
}

出典情報

Medium

iPhone/iPadアプリ開発逆引き大全500の極意

  • 著者: 増田 智昭, 池谷 京子

コメント

    コメントはありません