title: Sound does not play on iPad speaker but works fine on headphones and on the iPod Touch/iPhone speakers
body: <p>I know this is a question has been asked for many times, and I have checked most of the answers related in SO, but I have no luck finding the right answer to my problem.</p>
  
  <p>Here is the problem:</p>
  
  <p>I tried to play a mp3 file (just 2 seconds at most) in a game when some event is triggered, and I use the AudioPlayer to do so, below is the code blocks:</p>
  
  <pre><code>NSError *error;
  AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"ding" withExtension: @"mp3"] error:&amp;error] autorelease];
  if (error)  {
      NSLog(@"Error creating audio player: %@", [error userInfo]);
  }
  else {
      BOOL success = [audioPlayer play];
      // This always is "Play sound succeeded"
      NSLog(@"Play sound %@", success ? @"succeeded" : @"failed");
  }
  </code></pre>
  
  <p>When I ran this code in iPhone 4s, iTouch 3/4, the sound always played well and clear, but in iPad 1 or iPad2, there is no sound out from speaker. But when I plugged in my headphone, weird thing happened that there is sound from my headphone! The iPad is not in mute mode and the URL is correct.</p>
  
  <p>I am confused why this happened.</p>
  
  <p>PS: I tried the following code (got from <a href="https://stackoverflow.com/questions/6901363/detecting-the-iphones-ring-silent-mute-switch-using-avaudioplayer-not-worki">HERE</a>) to output the audio output port type:</p>
  
  <pre><code>CFDictionaryRef asCFType = nil;
  UInt32 dataSize = sizeof(asCFType);
  AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &amp;dataSize, &amp;asCFType);
  NSDictionary *easyPeasy = (NSDictionary *)asCFType;
  NSDictionary *firstOutput = (NSDictionary *)[[easyPeasy valueForKey:@"RouteDetailedDescription_Outputs"] objectAtIndex:0];
  NSString *portType = (NSString *)[firstOutput valueForKey:@"RouteDetailedDescription_PortType"];
  NSLog(@"first output port type is: %@!", portType);
  </code></pre>
  
  <p>When I plugged in my headphone, the output was <strong>"first output port type is headphone!"</strong> and when I unplugged it , the output turned out to be <strong>"first output port type is speaker!"</strong></p>
  
  <p>It would be great is someone can offer some help or advice.</p>