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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With