Playing back audio using AVAudioPlayer iOS 7

前端 未结 3 815
闹比i
闹比i 2020-12-02 02:55

I\'m struggling to follow Apple\'s documentation regarding playing back a small .wav file using the AVAudioPlayer class. I\'m also not sure what t

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 03:54

    What I do is create an entire miniature class just for this purpose. That way I have an object that I can retain and which itself retains the audio player.

    - (void) play: (NSString*) path {
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
        NSError* err = nil;
        AVAudioPlayer *newPlayer =
            [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: &err];
        // error-checking omitted
        self.player = newPlayer; // retain policy
        [self.player prepareToPlay];
        [self.player setDelegate: self];
        [self.player play];
    }
    

提交回复
热议问题