Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sound does not play on iPad speaker but works fine on headphones and on the iPod Touch/iPhone speakers

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.

like image 570
OriginalWood Avatar asked Dec 11 '12 01:12

OriginalWood


People also ask

Why does my sound only work with headphones on iPad?

That's true — the problem is that your iPad thinks headphones are plugged in. This occasionally happens when lint, dirt, liquid, or other debris gets stuck inside the headphone jack. You can quickly check to see if your iPad is stuck in headphones by pressing the volume buttons again.

Why is there no sound from my iPad speakers?

Go to Settings > Sounds (or Settings > Sounds & Haptics), and drag the Ringer and Alerts slider back and forth a few times. If you don't hear any sound, or if your speaker button on the Ringer and Alerts slider is dimmed, your speaker might need service.

Why does my iPhone sound only work with headphones?

At this point, this problem is being caused by one of two possibilities: Debris stuck inside the headphone jack or Lightning port is fooling your iPhone into thinking that headphones are plugged in. The headphone jack or Lightning port is damaged, either physically or by liquid.


1 Answers

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.

like image 63
Jon Brooks Avatar answered Oct 12 '22 23:10

Jon Brooks