0

I have a ViewController : GLKViewController with this code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"background-music-aac" ofType:@"caf"];
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];

    AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:data error:nil];
    audio.delegate = self;
    audio.volume = 1.0;
    audio.numberOfLoops = -1;

    [audio prepareToPlay];
    [audio play];

    [self setupContext];
    [self setupDisplay];
}

However... my graphical things working fine but the iOS simulator plays no music. did I something wrong?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1882812
  • 936
  • 5
  • 18
  • 41

1 Answers1

1

Assuming you are using ARC, the player is getting deallocated as soon your method returns to its caller. To quote from this answer:

ARC inserts a release call to the audio player, so it's deallocated right after leaving the method where it is created.

Try implementing the AVAudioPlayer as a property.

If your data is nil, the file was not properly added to the project. To add your file to the project, just drag and drop it and add it to your targets.

Also, AVAudioPlayer has a method, initWithContentsOfURL:, which you might want to use.

Community
  • 1
  • 1
duci9y
  • 4,128
  • 3
  • 26
  • 42
  • Making a property and using initWithContentsOfURL changes nothing :( – user1882812 Mar 23 '13 at 13:01
  • This is not the cuase of the problem. Where do you see the player being deallocated ? – giorashc Mar 23 '13 at 13:26
  • @giorashc The player is not retained. This is well known and has been answered previously. http://stackoverflow.com/questions/8016765/avaudioplayer-not-playing-any-sound – duci9y Mar 23 '13 at 16:21
  • point is how do you know he uses ARC ? and he said that the data passed to the player is nil – giorashc Mar 23 '13 at 17:16
  • @giorashc No release calls. And even if the user gets the `data` right, the player won't play. – duci9y Mar 23 '13 at 17:36
  • First of all he might forgot to release. My point is that your answer assumes to much that it might be not be relevant (in the case he does not use arc). That is why there are comments in which we ask the OP about the assumptions we make before answering. – giorashc Mar 23 '13 at 17:45
  • @giorashc You are assuming too much about the extent of my assumptions. Let the OP decide if it is accurate. – duci9y Mar 23 '13 at 18:54
  • @duci9y for first, throwing terms like "deallocated as soon as it is instatiated" tells nothing to the OP. You are not even mentioning why (ARC) and without the why the OP will continue asking questions like this. The idea here is to provide instructive help not just to get some points. – giorashc Mar 23 '13 at 19:01
  • @giorashc Again, you are assuming the OP will continue asking questions instead of reading the comment thread. Nevertheless, I edited my answer. If it still doesn't satisfy you, feel free to edit my answer or post your own. – duci9y Mar 23 '13 at 19:07