ほげほげ

プログラミング、英会話、ヨガ、料理などの備忘録など。

AVAudioPlayer

method内でローカル変数として使用しているだけだど、音声が流れない。
propertyで保持する必要がある。

    NSError *error = nil ;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"hoge" ofType:@"mp3"] ;
    NSURL *url = [NSURL fileURLWithPath:path] ;

    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if(error){
        NSLog(@"%@",error) ;
        return ;
    }
    
    [audioPlayer prepareToPlay];
    [audioPlayer play] ;
    self.audioPlayer = audioPlayer ; // これが必要だった