AVAudioPlayer plays very low volume audio on iPhone 6 and 6+

I play sound using AVAudioPlayer .

The sound on iPod (iOS7 and iOS8 both) is good.

But when I play the same sound on the iPhone, the sound plays at a very low volume.

Here is my code:

 var audioPlayer: AVAudioPlayer? var soundURL:NSURL? = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "mp3")!) audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil) audioPlayer?.prepareToPlay() audioPlayer?.volume = 1 audioPlayer?.play() 

The AudioToolbox.framework library is already included in my project.

How to improve sound in iPhone6 ​​and 6+?

EDIT

I recently noticed that the sound automatically increased by a couple of seconds, but I don’t know what happened.

+7
ios swift avaudioplayer
source share
3 answers

I think if you set the volume of AVAudioPlayer to 1, the problem will be on the system volume. When playing sound, you can use a different category of sound, and the volume is different in different categories or in different audio tracks (for example, on media, a headset or bluetooth). The default category in your application will be ambient , but the music application will play . You can try to adjust the volume when playing a sound, not before playing it.

+2
source share

The volume is low because the sound comes through the top speaker on the iPhone (used for calling).

You must cancel the port transfer of the AVAudioSession to the main speaker. (bottom speaker)

This is the code that transmits the audio output to the speaker.

 AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker, error: &error) 

Swift 3.0 :

 do { try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker) } catch _ { } 
+34
source share

You also need to increase the amount of media.

 extension MPVolumeView { var volumeSlider:UISlider { self.showsRouteButton = false self.showsVolumeSlider = false self.hidden = true var slider = UISlider() for subview in self.subviews { if subview.isKindOfClass(UISlider){ slider = subview as! UISlider slider.continuous = false (subview as! UISlider).value = AVAudioSession.sharedInstance().outputVolume return slider } } return slider } } 

then change the volume as follows:

 func setMediaVolume(volume : Float) { MPVolumeView().volumeSlider.value = volume } 

in your case, setMediaVolume (1) right before playing the sound.

also make sure that the current audio route matches the speaker, not the speaker, you can do this using

 try! AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker) 
0
source share

All Articles