せでぃのブログ

ブログ初心者おいどんのどうでもいい愚痴やどうでもいい愚痴やどうでもいいマメ知識などを披露するチラシの裏です。

効果音を鳴らす

(iPhone 開発メモ) 短い音を再生する方法
このページほぼそのままで動いた。


1.音声ファイルの準備
鳴らしたいa_sound.aiffというファイルを用意しておく。
今回はこちらのサイトAIFF Audio Testから拝借した。

2.xcode起動
適当にsingle view appliあたり選択。
プロジェクト名は適当にsoundtestという名前で作成してみた。
f:id:Sediment:20130726082515p:plain
f:id:Sediment:20130726082536p:plain

3.AudioToolbox.frameworkフレームワーク追加
プロジェクトのフォルダでBuild Phasesを選択。
Link Bynary With Libraryを選択して、+ボタンを押す。
AudioToolbox.frameworkを選択してAdd。
f:id:Sediment:20130726082706p:plain
f:id:Sediment:20130726083436p:plain
f:id:Sediment:20130726083456p:plain

4.用意した音声ファイルの取り込み
プロジェクトフォルダ内にあるSupporting Filesフォルダで右クリックして、Add Files toを選択。
用意した画像ファイルを選択して、Add。
※元ファイルを削除してみるとエラーになるのでわかるけども、ここではファイルへの参照を作成しているだけなので、アプリとしてフォルダを移動したりサーバ上へアップロードする場合は、必ず、プロジェクトフォルダの中へ実際のファイルを移動しておいてからこの作業を行う必要があるので、注意。
f:id:Sediment:20130726083737p:plain
f:id:Sediment:20130726083748p:plain

5.再生用のボタン追加
Interface Builderの画面に移り、Round Rect Buttonをドラッグアンドドロップ。表示名をPlayと変える。
Assistant Editorで、オブジェクトをViewController.hに入れ込む。作成したボタンを@interfaceの次の行にドラッグアンドドロップし、ConnectionをAction、Nameをplay、EventはTouch Up Insideにして、Connect。
f:id:Sediment:20130726083854p:plain
f:id:Sediment:20130726084118p:plain

6.コードの入力
入力前と後の画像を張ってみる。テキストはページ下部に置く。
ViewController.h
入力前
f:id:Sediment:20130726084435p:plain
入力後
f:id:Sediment:20130726084602p:plain
ViewController.m
入力前
f:id:Sediment:20130726084625p:plain
入力後
f:id:Sediment:20130726084752p:plain

7.run
ボタンを押すたびに音が再生される。
f:id:Sediment:20130726084821p:plain


コード部分
ViewController.h

//
//  ViewController.h
//  soundtest
//
//  Created by macbook on 2013/07/26.
//  Copyright (c) 2013年 macbook. All rights reserved.
//

#import <UIKit/UIKit.h>
#include <AudioToolbox/AudioToolbox.h> 

@interface ViewController : UIViewController {
    CFURLRef        soundURL;
    SystemSoundID   soundID;
}

@property (readwrite)   CFURLRef        soundURL;
@property (readonly)    SystemSoundID   soundID;
- (IBAction)play:(id)sender;

@end


ViewController.m

//
//  ViewController.m
//  soundtest
//
//  Created by macbook on 2013/07/26.
//  Copyright (c) 2013年 macbook. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize soundURL;
@synthesize soundID;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    CFBundleRef mainBundle;
    mainBundle = CFBundleGetMainBundle ();
    
    //音声ファイルのファイル名と拡張子をここで記述CFStringRef型
    soundURL  = CFBundleCopyResourceURL (mainBundle,CFSTR ("a_sound"),CFSTR ("aiff"),NULL);
    AudioServicesCreateSystemSoundID (soundURL, &soundID); //サウンドIDを取得する
    CFRelease (soundURL); //破棄
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)play:(id)sender {
    AudioServicesPlaySystemSound (soundID); //音声ファイルの再生をおこなう
}
@end


参考)
(iPhone 開発メモ) 短い音を再生する方法


解説や注意等
iPhoneアプリ開発雑記帳-サウンドを再生する
A day In The Life-iOS 組み込みの効果音を鳴らしてみる
アプリで音を鳴らす方法(簡単な方法編)
AudioServicesCreateSystemSoundID/AudioServicesPlaySystemSoundで音を鳴らす
TECHLOG-効果音の再生