views:

1004

answers:

3

I initialize an AVAudioPlayer instance:

NSString* path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"wav"];

AVAudioPlayer* player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; //Returned object not nil, indicating that the file has loaded successfully

BOOL b = [player prepareToPlay]; //returns TRUE

BOOL b2 = [player play]; //returns TRUE, goes immediately to the next line since asynchronous

[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]; //wait 1 sec

printf("%10.4f\n",player.duration); //duration is 2s

printf("%10.4f\n",player.currentTime); //position remains 0

/* There is no sound output, player is also stuck since position==0 */

Does anyone know why the AVAudioPlayer is not responding? Is there something that I am overlooking while initializing, playing the audioplayer? Boilerplate code maybe?

I am running this on the iPhone simulator. Also, AudioServicesPlaySystemSound() works for the same file, which is puzzling, but this indicates that sound playback might work with AVAudioPlayer as well.

+2  A: 

I have 2 quick suggestions:

  1. Check to see if that path actually gives you data. Alloc a NSData object using [[NSData alloc] initWithContentsOfFile:path] and inspect it in the debugger to see if you're actually loading that wav file.

  2. I wrote a sample project that uses AVAudioPlayer here: AVAudioPlayer Example. As the code is pretty much the same as yours, the only thing I can imagine is that there is a problem with your data.

Check those out and see if it gets you anywhere!

Jeffrey Forbes
A: 

I only ask why you are sleeping?

I would try removing that unless you have some reason for including it (maybe just for debugging?).

Corey Floyd
+1  A: 

I have just had a similar problem. I am not sure if it is the same solution, but my application records audio through the iphone and then plays it back to the user.

I thought I was doing the correct thing by telling my audio session that it was going to record data, so I did the following:

[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

Using this setting playback worked on the Simulator but not on the device.... the didFinishPlaying event never got raised.

I removed this line after realising I didn't need it an audio playback started working correctly. Now I just need to work out why it is so quiet :)

Paul

Kinlan