Why don't I hear music?

71 views Asked by At

So I have an app where I want to include background music. I use the following code for the music:

.h: (I did also add the framework)

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController
{
    AVAudioPlayer *player;
}

.m:

- (void)viewDidAppear:(BOOL)animated {

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"song" withExtension:@"mp3"];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    player.numberOfLoops = -1;
    [player play];
    NSLog(@"playing music");
}

The path for the music file is definitly correct. The app does not crash but I just do not hear any music.

By the way: the LogMessage ("playing music") appears in the debugger.

Any ideas?

1

There are 1 answers

0
Sarath Neeravallil On

Add prepareToPlay method before [player play];

[player prepareToPlay];

Hope this will work...