Sound does not play on iPad speaker but works fine on headphones and on the iPod Touch/iPhone speakers
avaudioplayer, ios, ipad
Solution
There is a code change solution to this, but also an end-user solution: turn the 'Ring/Silent switch' to on. Specifically, the problem is that the default setting of AVAudioSessions, `AVAudioSessionCategorySoloAmbient`, is to be silent if the phone is in silent mode.
As mentioned by the original poster, you can override this behavior by calling:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
AVAudioSession Reference recommends setting the `AVAudioSessionCategoryPlayback` category:
For playing recorded music or other sounds that are central to the successful use of your app.
Problem
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. Here is the problem: 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: ``` NSError *error; AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"ding" withExtension: @"mp3"] error:&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"); } ``` 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. I am confused why this happened. PS: I tried the following code (got from HERE) to output the audio output port type: ``` CFDictionaryRef asCFType = nil; UInt32 dataSize = sizeof(asCFType); AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &dataSize, &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); ``` When I plugged in my headphone, the output was "first output port type is headphone!" and when I unplugged it , the output turned out to be "first output port type is speaker!" It would be great is someone can offer some help or advice.