Xcode 開発で、BGM(サウンド)を使用するときの設定をまとめました。
ゲームアプリで、BGMを使用したいとき、適切なタイミングで処理を定義します。
ViewController.hの設定
1 | #import <AudioToolbox/AudioToolbox.h> |
3 | @interface ViewController : UIViewController{} |
5 | @property AVAudioPlayer *BGM_BK; |
ViewController.mの設定
1 | #import <AudioToolbox/AudioToolbox.h> |
3 | @interface ViewController : UIViewController{} |
5 | @property AVAudioPlayer *BGM_BK; |
ViewController.mの設定
1 | @interface ViewController ()<AVAudioPlayerDelegate> |
14 | NSString *path = [[ NSBundle mainBundle] pathForResource: @"BGM1234" ofType: @"mp3" ]; |
15 | NSURL *url = [ NSURL fileURLWithPath:path]; |
16 | self .BGM_BK = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: NULL ]; |
18 | self .BGM_BK.numberOfLoops = -1; |
19 | self .BGM_BK.volume = 0.18f; |
20 | self .BGM_BK.currentTime = 0.0f; |
BGM:BGM1234.mp3
numberOfLoops:再生回数。-1は、エンドレス設定