Question

I'm attempting to use the below code to play an mp3 file. It seems to run fine but no sound comes from the phone. The volume is up, the file is found (if I change the requested file to a name that doesn't exist I get an error), and no errors show up (even in the "playerError"). The only thing I see that might be a problem is perhaps the player deallocating before it finishes playing, but I'm not sure off hand how to fix that. Any suggestions as to what the problem might be and how to fix it? Thank you much!

...

if([responseString  isEqual: @"Stop"]){
    [self playStop];
}

...

- (void) playStop{
    NSString *soundFilePath =
    [[NSBundle mainBundle] pathForResource: @"stop"
                                    ofType: @"mp3"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

    NSError *playerError;

    AVAudioPlayer *newPlayer =
    [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                           error: &playerError];
    [newPlayer play];
}
Was it helpful?

Solution

If you make your "AVAudioPlayer" an instance variable or a property (instead of a local variable within the function), so that it sticks around instead of being immediately released by ARC when "playStop" finishes, you will likely have much better luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top