0

I have the following code and the sound_file is found. Also I get no errors. The audio file is added to the bundle. But I get no audio out on the iOS simulator.

- (void)playNotificationSound
{
    NSString *sound_file;
    AVAudioPlayer * audioPlayer;

    sound_file = [[NSBundle mainBundle] pathForResource:@"onebell" ofType:@"caf"];
    if (sound_file){

        NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        //audioPlayer.delegate = self;
        audioPlayer.volume = 1.0f;
        [audioPlayer prepareToPlay];
        [audioPlayer play];
    }
}
4

1 に答える 1

0

への強力な参照が必要ですAVAudioPlayer。ARCを使用しているため、実行が終了するAVAudioPlayerとメモリに保存されませんplayNotificationSound。また、それへの文字列参照により、割り当てが解除されるのを防ぐことができます。

を追加して、新しい変数を宣言する代わりに@property (strong, nonatomic) AVAudioPlayer *audioPlayer;使用します。self.audioPlayerAVAudioPlayer

于 2013-09-17T02:47:50.887 に答える