AVAudioPlayer playing Sound with very low volume in iPhone 6 and 6+

Mohammad Zaid Pathan picture Mohammad Zaid Pathan · Apr 14, 2015 · Viewed 17.2k times · Source

I am playing a sound using AVAudioPlayer.

The sound volume in iPods (iOS7 & iOS8 both) is good.

But when I play same sound in iPhones the sound is playing with 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()

I've already included AudioToolbox.framework library in my project.

How can I improve sound in iPhone6 and 6+ ?

EDIT

Recently i noticed that automatically the sound was increased for couple of seconds,but don't know what's wrong happening.?

Answer

Vipin Johney picture Vipin Johney · Jul 22, 2015

The volume is low because the sound is coming via the top speaker in iPhone(used for calling).

You have to override the AVAudioSession out put port to the main speaker.(bottom speaker)

This is the code that Route audio output to speaker

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

Swift 3.0:

do {
     try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
   } catch _ {
}