1

There's could be a in my apps cause when I tried to build it in iPhone device, i cannot hear a sound and i couldn't also use the volume button of the device.. while when i build it in simulator it works perfectly.. I tried using the code below but it didn't work also.. Can somebody help me in here.. Thanks..

 UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);


  NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"sounds" ofType:@"wav"];
AVAudioPlayer *soundPlay = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL];
soundPlay.volume = 1.0;
self->puzzSFX = soundPlay;
soundPlay.numberOfLoops = 0;
if ([soundPlay currentTime] != 0) [soundPlay setCurrentTime:0];
[soundPlay play];
soundPlay.delegate = self;

PS: I tried to adjust the volume through the headphone and then poof it will start working..

jrf
  • 72
  • 9
  • Are you sure the problem is in your code, and not your iPhone? Try downloading a simple free app that makes sounds and play with it. Does it have the same problem? Or test your code on another iPhone. Ideally, both. – abarnert Sep 28 '12 at 07:20
  • I tried using other apps, the audio and the volume button works fine.. – jrf Sep 28 '12 at 07:24
  • I'm running into a same issue; simulator working great, device doesn't get any recorded audio...wish this question had been answered all those years ago. – TahoeWolverine Jan 27 '17 at 19:20

1 Answers1

0

The problem may have to do with the conversion from an NSString path to NSURL, where you need to check that special URL characters get escaped (chars that you may not have in the simulator, but only on the device). Try to do this:

NSURL *fileURL = [NSURL URLWithString:[soundPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Then initing your AVAudioPlayer with this NSURL.

micamoita
  • 450
  • 2
  • 14