Zaid Pathan Zaid Pathan - 1 month ago 28
iOS Question

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

Answer

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.

Most probable issue is with audio session . Try these below line of codes

        do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
    }  catch {
        print("AV session error")
    }