Zaid Pathan
Zaid Pathan

Reputation: 16820

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

Upvotes: 25

Views: 23241

Answers (5)

Bohdan Savych
Bohdan Savych

Reputation: 3447

Solution for Swift 4

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

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

Upvotes: 15

Pengguna
Pengguna

Reputation: 4941

I also experienced this in the swift playground, the volume is much smaller than the sound in the original file. Just increase the volume.

audioPlayer?.volume = 3

Upvotes: 3

Vipin Johney
Vipin Johney

Reputation: 1333

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 _ {
}

Upvotes: 71

Hammadzafar
Hammadzafar

Reputation: 488

you need to maximize the media volume as well.

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 like this :

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

in your case, setMediaVolume(1) right before you play your audio.

also make sure that the current audio route is to the Speaker and not the earpiece, you can do that using

try! AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker)

Upvotes: 2

user3349433
user3349433

Reputation: 480

I think if you set the volume of AVAudioPlayer to 1, then the problem is on the system volume. You may use a different audio category when playing your sound and the volume is different in different category or different audio route(like native speaker, headset or bluetooth) The default category in your app will be ambient, but the Music app's is playback. You can try to adjust your volume when you are playing your sound rather than before it's played.

Upvotes: 7

Related Questions