user979331
user979331

Reputation: 11951

Objective-C - Trying to play sound

I am trying to play a sound in my app with the following code:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"errorbeep.wav" ofType:@"m4a"];
        NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
                                                                       error:nil];
        player.numberOfLoops = -1; //Infinite

        [player play];

But the sound does not play :(

after debugging, I can see that soundFilePath is nil Here is a screenshot of my Resources file:

enter image description here

Upvotes: 0

Views: 261

Answers (3)

Sachin
Sachin

Reputation: 367

Select your sound file and see if you have its Target Membership Checked for your target.

enter image description here

Upvotes: 0

Jteve Sobs
Jteve Sobs

Reputation: 244

You should import the file to your project with one extension. Thats probably what's going on. Just re-import the file as one extension (i.e, errorbeep.m4a)

Upvotes: 1

dijipiji
dijipiji

Reputation: 3099

If the file path is nil I would try renaming the file to just errorbeep.m4a and then doing:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"errorbeep" ofType:@"m4a"];

Upvotes: 0

Related Questions