2012-03-15 9 views
0

はその後、私は10秒が完了するまで待機した後、第2 1を開始したいです。NSSoundを再生する2回

しかし、私はすでに二度同じNSSoundを再生する問題を抱えています。

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"MessageArrived" ofType:@"wav"]; 
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES]; 
[sound play] //It plays the fist time 
[sound play] //I get the following Error and it doesn't play a secont time 
/*malloc: *** auto malloc[498]: error: GC operation on unregistered thread. Thread registered implicitly. Break on auto_zone_thread_registration_error() to debug.*/ 

このエラーの原因を教えてもらえますか? どうすれば対応できますか?

これを行う別の方法はありますか? 2番目のplayメソッドを呼び出したときに音が既に再生され

+0

ガベージコレクションを使用しているようです。可能であれば、代わりにコードをARCに移動する必要があります.GCを有効にすると、多くのフレームワークが壊れやすくなります。 –

+0

ARCを有効にしてGCを無効にするにはどうすればよいですか? 私はこのプロジェクトを開始しました。ARCは利用可能でした... – mabstrei

+0

新しいXcode 4.3.1には、** Edit> Refactor **メニューの下で、GCからARCへのプロジェクトの自動変換があります。 –

答えて

1

...

NSSound *sound = [NSSound soundNamed:@"MessageArrived"]; 
BOOL res = [sound play]; 
NSLog(@"%d", res); 
[NSThread detachNewThreadSelector:@selector(playSecondSound:) toTarget:self withObject:sound]; 

-(void)playSecondSound:(NSSound*)sound 
{ 
    [NSThread sleepForTimeInterval:10-[sound duration]]; 
    [sound stop]; 
    BOOL res = [sound play]; 
    NSLog(@"%d", res); 
} 
私は音が終了した場合であっても、私が呼び出す必要があることが分かった

[音を止める]を押す。

0

、第二のプレイなど、ガベージコレクションで明らかにバグが無視されるべきであるが、それはあなたが達成したいとの問題ではありません。検索し、それを修正しようとしているの戦利品は、私は私の問題の解決策を得たので、後

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"MessageArrived" ofType:@"wav"]; 
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES]; 
[sound play]; 

//Will play the second time after 10.0 seconds have passed. 
NSInteger wait = ([sound duration] < 10.0) ? 10.0 - [sound duration] : 0.0; 
[sound performSelector:@selector(play) withObject:nil afterDelay:wait]; 
+0

はうまくいくかもしれないが、私はまだ「malloc関数を得る:***自動malloc関数[589]:エラー:未登録のスレッドでGC操作を(暗黙的に登録されたスレッドauto_zone_thread_registration_errorにブレーク)をデバッグする。 2番目のものが再生されるべきであるときのメッセージ...したがって、secontは音がありません... – mabstrei

+0

エラーメッセージは、10.6で追加されたガベージコレクションに関する警告に過ぎず、問題ではありません。これはあなたがコントロールできないという[Appleが解決しなければならない]バグです(http://stackoverflow.com/questions/7010233/gc-operation-on-unregistered-thread)。フレームワークの中には、特にフレームワークの64ビット版と比較して、32ビットコードのものがいくつかあります。 – Conor

+0

しかし、2番目のサウンドは再生されません...私は最初のものを聞くことができますが、2番目のものが欠けています... – mabstrei

関連する問題