Playing back audio using AVAudioPlayer iOS 7

前端 未结 3 796
闹比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:46

    You're having an ARC issue here. myPlayer is being cleaned up when it's out of scope. Create a strong property, assign the AVAudioPlayer and you're probably all set!

    @property(nonatomic, strong) AVAudioPlayer *myPlayer;
    
    ...
    
    // create new audio player
    self.myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
    [self.myPlayer play];
    
    0 讨论(0)
  • 2020-12-02 03:49

    Is the filePath coming back with a valid value? Is the fileURL coming back with a valid value? Also, you should use the error parameter of AVAudioPlayer initWithContentsOfURL. If you use it it will likely tell you EXACTLY what the problem is.

    Make sure you check for errors and non-valid values in your code. Checking for nil filepath and fileURL is the first step. Checking the error parameter is next.

    Hope this helps.

    0 讨论(0)
  • 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];
    }
    
    0 讨论(0)
提交回复
热议问题