0

私は常にオーディオ ツールボックスを使用してサウンドを再生してきましたが、ユーザーはサウンドがないと言っています。これは、サイレント モードでは再生されないためです。代わりに av ファンデーションに交換しようと何度も試みましたが、うまくいきません。これは私が今使用しようとしているコードです:

- (IBAction)soundbutton:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"EASPORTS" ofType:@"mp3"];
AVAudioPlayer * theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

[theAudio play];}
4

1 に答える 1

3

理由としては、ARC でオーディオ プレーヤーが ARC によってリリースされている可能性があります。そのため、 を強力に参照する必要があります。 as クラス レベル プロパティAVAudioPlayerを作成することでそれを行うことができます。AVAudioPlayerこれがあなたの .hファイルで、このような方法です

@property (strong, nonatomic) AVAudioPlayer *theAudio;

そして、.mファイルでこのように合成します

@synthesize theAudio;

最後に、コードは次のようになります

- (IBAction)soundbutton:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"EASPORTS" ofType:@"mp3"];
    self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[self.theAudio play];
}

デリゲート メソッドが何かに応答しているかどうかも確認してください

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;
于 2013-02-28T19:34:03.027 に答える