せでぃのブログ

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

SleepSound2

Sleep Sound
このページにあるアプリを動かそうとしたところ、エラーが出たので直してみた。
何はともあれ、「SleepSound.zip」をダウンロードして、xcodeで開く。


SoundEngine.cppというファイルで次のerror。Goto into protected scope。
エラーの名前でググったところ、xcode上でC++のswitch構文に修正が必要とのこと。でも該当箇所ではswitch構文など使っていない。
Switch case is in protected scope
要はC++では正しいはずのファイルをxcodeが勝手にエラー判定しちゃっているという状況だろうと推測して、次のリンクを参考にビルド設定を変えてみたらどうかと思ったが、それでもダメだった。
C++を使えるようにする
後日追記:SoundEngine.cppのエラー行をコメントアウトして動いた

そもそも、今時こういう形でBGM鳴らさないよなぁというところで、BGMの鳴らし方をガラっと変える形で対応してみた。


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

2.コードの入力
MainView.mの#include部分と- (void) startBackgroundMusicメソッド部分で、古い不要な部分をコメントアウトして、新しく挿入する。
f:id:Sediment:20130808222606p:plain

MainView.m

#import "MainView.h"
#import <QuartzCore/QuartzCore.h>
//#import "SoundEngine.h"
#include <AVFoundation/AVFoundation.h> 

SoundEngine.hの読み込みを辞めて、追加したAVFoundationを読み込む。

- (void) startBackgroundMusic{
//新規追加部分
    NSString *path = [[NSBundle mainBundle] pathForResource:@"oceanwaves" ofType:@"m4v"];
    NSURL *url = [NSURL fileURLWithPath:path];
    AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [audio play];
/*既存部分
	NSBundle* bundle = [NSBundle mainBundle];
	
	// Note that each of the Sound Engine functions defined in SoundEngine.h return an OSStatus value.
	// Although the code in this application does not check for errors, you'll want to add error checking code 
	// in your own application, particularly during development.
	//Setup sound engine. Run  it at 44Khz to match the sound files
	SoundEngine_Initialize(44100);
	// Assume the listener is in the center at the start. The sound will pan as the position of the rocket changes.
	SoundEngine_SetListenerPosition(0.0, 0.0, kListenerDistance);
	// Load each of the four sounds used in the game.
	SoundEngine_LoadBackgroundMusicTrack([[bundle pathForResource:@"oceanwaves" ofType:@"m4v"] UTF8String], true, true);
	
	//Play start sound
	SoundEngine_StartBackgroundMusic();
*/
    
}

(void)startBackgroundMusic内の既存部分を全てコメントアウトして、新規で挿入。

3.AudioSupportディレクトリへの参照を削除
エラー箇所のせいでこのままだとどうしてもコンパイルできないので、エラーが出ているファイルへの参照を辞める。
AudioSupportディレクトリでCtrol+クリックで、Deleteを選択。
Remobe Referencesを選択。
f:id:Sediment:20130808223201p:plain
f:id:Sediment:20130808223231p:plain

4.Run
これで動くはず。
f:id:Sediment:20130808222126p:plain


参考)
(iPhone 開発メモ) AVAudioPlayer を使用して音楽を再生する