Reputation: 93
I've went to COUNTLESS links and tutorials for this, but I either get no errors but no sound plays, or I get an error.
Using the code
NSString *toneFilename = [[NSBundle mainBundle] pathForResource:@"spoken" ofType:@"mp3"];
NSURL *toneURLRef = [NSURL fileURLWithPath:toneFilename];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: toneURLRef error: nil];
player.currentTime = 0;
player.volume = 1.0f;
[player play];
My application runs, but doesn't play the sound on load. I was listening to music and tried the app, and it stopped my music as if it were going to play something in-app, but it never did.
Why is this happening?
Upvotes: 1
Views: 63
Reputation: 34839
If you're using ARC, and you declare an AVAudioPlayer
as a local variable, the player will be dealloc'ed by ARC before it has a chance to play the sound. To fix the problem, declare the player as a property.
Upvotes: 1