user3818701
user3818701

Reputation: 93

iOS not playing my sound?

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

Answers (1)

user3386109
user3386109

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

Related Questions