2011-09-13 7 views
0

私のサウンドボードアプリでは、しばらくすると、アプリケーションが終了して再び開くまで音が止まります。何が間違っているのか分からない!ここに私のコードは次のとおりです.Mファイルでサウンドボード(Xcode)での動作が停止する

(imported files here) 

@interface MainView : UIView { 

} 
- (IBAction)pushButton2:(id)sender; 

@end 

:.hファイルで

(imported files here) 


@implementation MainView 

- (IBAction)pushButton2:(id)sender { 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"]; 
    AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    [theAudio play]; 

} 

@end 
+0

デリゲートの方法が分かります。 mp3の終了前に音が止まりますか? –

+0

しばらくすると音が止まります。 – McCadi

+0

でも、デリゲートメソッドが役立ちます。 –

答えて

1

私はそれはあなたが見ている行動を起こすかわかりません、ボタンを押すたびに必ずAVAudioPlayerがリークしています。さらに、ボタンを押す度に押すのではなく、一度ロードするだけです(たとえば、viewDidLoad)。おそらく次のようなものがあります。

@interface MainView : UIView 
{ 
    AVAudioPlayer* audioPlayer; 
} 
- (IBAction)pushButton2:(id)sender; 
@end 

@implementation MainView 

- (void) viewDidLoad 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    audioPlayer.delegate = self; 

} 

- (void) viewDidUnload 
{ 
    [audioPlayer release], audioPlayer = nil; 
} 

- (IBAction)pushButton2:(id)sender 
{ 
    [audioPlayer play]; 
} 

@end 
+0

ありがとうございました!別のサウンドを追加すれば、どのように見えますか? – McCadi

+0

サウンドごとにAVAudioPlayerインスタンスを1つだけ持つことができます。 – zpasternack

+0

私はすべての私の音を止めることに問題がありました。私はちょうどすべての異なったオーディオプレーヤーを作りました、そして、私のストップアクションで私はそれらのすべてを止めました。私のために働いた! – msweet168

関連する問題