2012-05-01 8 views
0

バックグラウンドミュージック

#import "AVFoundation/AVFoundation.h" 

は、だから私は問題を抱えて、私は

NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; 
NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 
player.numberOfLoops=-1; 
[player play]; 
(つまり、最初にロードされているものである)私のクラスの初期化メソッドで最初にこのコードを試してみました

しかし、私はスレッドでそれを行うことを決めたよりも、それが、うまくいきませんでした: initメソッドで:

[NSThread detachNewThreadSelector:@selector(playMusic) toTarget:self withObject:nil]; 

と方法itse lf:

-(void) playMusic { 
    NSString* soundFilePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; 
    NSURL* soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
    AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 
    player.numberOfLoops=-1; 
    [player play]; 
} 

もちろん* .hファイルで宣言されています。それもうまくいかなかった。

私はplayMusic方式のライン

} 

上のファイルが2または3秒間再生し、その後、停止している、デバッグしようとしました。デバッグなしでは再生されません。何が問題ですか?

+0

AVAudioPlayerを、o_O に感謝したクラス – Felix

+0

mgのインスタンス変数にしてください))) – Alexander

答えて

2

AVAudioPlayerの割り当てが直ちに解除されます。あなたはプレーヤーを保持する必要があります。 AVAudioPlayerをクラスのインスタンス変数にします。

関連する問題