Sound does not play on iPad, but works great on headphones and on iPod Touch / iPhone

I know this question has been asked many times, and I checked most of the answers related to SO, but I was not lucky to find the correct answer to my problem.

Here is the problem:

I tried to play an mp3 file (maximum 2 seconds) in the game when some event is triggered, and I use AudioPlayer for this, below are the blocks of code:

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 on the iPhone 4s, iTouch 3/4, the sound always sounded good and clear, but on the iPad 1 or iPad2 there is no sound from the speaker. But when I connected the headphones, it was strange that a sound was heard from my headphones! The iPad is not in mute mode, and the URL is correct.

I am confused why this happened.

PS: I tried the following code (obtained from HERE ) to output the type of audio output signal:

 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 the headphones, the output was "the first type of output port is headphones!" And when I turned it off, the output turned out to be the first type of speaker output port! "

It would be great if someone could offer some help or advice.

+4
source share
3 answers

There is a solution for changing the code for this, but also a solution for the end user: turn the β€œRing / Silent” switch to the β€œon” position. In particular, the problem is that the default setting for AVAudioSessions, AVAudioSessionCategorySoloAmbient , should be quiet if the phone is in silent mode.

As mentioned in the original post, you can override this behavior by calling:

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 

The AVAudioSession link recommends setting the AVAudioSessionCategoryPlayback category:

To play recorded music or other sounds that are central to the successful use of your application.

+5
source

To update the @JonBrooks answer above with Swift 2 syntax, I added the following to my viewDidLoad () to override the silent switch and get the sound:

 do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) } catch let error as NSError { print(error) } 
+2
source

Solution found! After I added this line of code before playing, the sound finally plays from the speaker

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 

Now I still do not understand why, and I will delve into it. :)

0
source

All Articles