Alberto Schiariti
Alberto Schiariti

Reputation: 1589

Objective-C - AVAudioPlayer

I found that using AVAudioFoundation should be the easiest way to play an mp3 on objective-c, but I don't know what I'm doing wrong here. No sound on either the device or the simulator. I imported the framework and all the other "normal" things, but it just keep not playing. What is wrong in this code?

NSString *path = [[NSBundle mainBundle] pathForResource:@"DuomoDiFirenze" ofType:@"mp3"];
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio prepareToPlay];
[theAudio play];

Upvotes: 1

Views: 948

Answers (2)

Vladimir Despotovic
Vladimir Despotovic

Reputation: 3505

So when you declare AVAudioPlayer * theAudio as a property of whichever object you are using it it, it will work. That's the only thing you are missing.

Upvotes: 0

Mattias Wadman
Mattias Wadman

Reputation: 11425

Do you compile with ARC? if so make sure to keep a reference to the AVAudioPlayer instance as else ARC will make sure to release it for you and the audio playback will stop immediately.

Upvotes: 2

Related Questions