Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.?

like image 292
Mohammad Zaid Pathan Avatar asked Apr 14 '15 10:04

Mohammad Zaid Pathan


People also ask

Why does my iPhone sound really low but it's on full sound?

By and large, low sound coming from your iPhone speaker is the result of faulty app settings. Make sure mono audio and equalizer settings are turned off, and remove any apps that may override other sound settings. Otherwise, it's possible your phone's speaker may be either dirty, damaged, or both.


2 Answers

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 _ {
}
like image 50
Vipin Johney Avatar answered Oct 10 '22 04:10

Vipin Johney


Solution for Swift 4

try? session.setCategory(.playAndRecord, mode: .default, policy: .default, options: .defaultToSpeaker)

P.S. For full class you can check my gist

like image 12
Bohdan Savych Avatar answered Oct 10 '22 03:10

Bohdan Savych