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

20,459

Solution 1

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.

Solution 2

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

Solution 3

Solution for Swift 4

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

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

Solution 4

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)

Solution 5

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
Share:
20,459

Related videos on Youtube

Mohammad Zaid Pathan
Author by

Mohammad Zaid Pathan

#SOreadytohelp Thank you for visiting my profile. I am helping solve real problems and develop highly scalable and reusable software. Experienced with: Mobile: iOS, React Native, Flutter, Android Web: Angular, React, J2EE Backend: NodeJS, J2EE Desktop: macOS Wearable: watchOS, wearOS DB: MySQL, Realm, SQLite, CoreData, Backendless, Firebase Server: AWS I am dedicated, hardworking & easy to deal with IT professional. My technical experience consist working with wide verity of full-stack technologies such as Native iOS, Angular, NodeJS, React-Native, ReactJS, J2EE, wearOS, watchOS, Flutter, macOS development. I have sound experience with AWS cloud services, such as EC2, S3, RDS, Lambda, Rekognition & DynamoDB. I have experience of giving 9 weeks on floor technical training to almost 80+ participants on React Native. I believe in open source and helping IT community grow; hence contribution on Stackoverflow led me to become top 3% contributor overall. I am having sound grip on team leading and project management. Having emotional intelligence, empathy, equality, truthfulness, professional ethics and humbleness helps me manage people, projects and myself better. I always look to connect with individuals who are having mindset to helps communities and humanity. I consider myself beginner in the IT industry and having burning desire to become a pioneer who help others grow.

Updated on August 02, 2020

Comments

  • Mohammad Zaid Pathan
    Mohammad Zaid Pathan almost 4 years

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

    • Michael
      Michael about 9 years
      You are not setting the volume of the whole device. Just depending on the mainVolume. Is the main volume on the same level??
    • Mohammad Zaid Pathan
      Mohammad Zaid Pathan about 9 years
      yes Volume is set on full.
    • Douglas Hill
      Douglas Hill about 9 years
      This seems silly, but is sound.mp3 not very loud?
    • Mohammad Zaid Pathan
      Mohammad Zaid Pathan about 9 years
      It's playing loud in iPods but not in iPhone 6 and 6+.
    • Michael
      Michael about 9 years
      And playing a Song in the Music-App is also the same Volume? Maybe the iPods are louder in general
    • Mohammad Zaid Pathan
      Mohammad Zaid Pathan about 9 years
      Yes Music-App volume is cool and louder.
    • Michael
      Michael about 9 years
      Could you try just adding .0 to your volume. -> audioPlayer?.volume = 1.0
    • Simon.
      Simon. about 9 years
      did the float value append fix this? I'm experiencing this issue (amongst a bag of others with AVAudio)
    • Mohammad Zaid Pathan
      Mohammad Zaid Pathan about 9 years
      no, Changin value to 1.0 didn't work.
  • Mohammad Zaid Pathan
    Mohammad Zaid Pathan about 9 years
    Can i change ambient to playback.?
  • user3349433
    user3349433 about 9 years
    Are you sure it's the case I mentioned above? The ambient sound won't play when background, but the playback will. You can try [AVAudioSession sharedInstance] setCategory:error:]. But you should attention that if your app is not a music player or something similar, apple may reject it.
  • user3349433
    user3349433 about 9 years
    You should know that even when playback category, your volume can still be adjust to a low value that is different to ambient or other categories.
  • Mohammad Zaid Pathan
    Mohammad Zaid Pathan about 9 years
    So i did set category to Playback, have you any reference saying that my app can be rejected if my app is not a music player or something similar, apple may reject it.
  • Mohammad Zaid Pathan
    Mohammad Zaid Pathan about 9 years
    My app is Recording and Playing sound simultaneously, When i set category to Playback recording is stopped. When i set category to PlayAndRecord sound volume again slow.
  • Mohammad Zaid Pathan
    Mohammad Zaid Pathan about 9 years
    Can't I use two category in One app.?
  • user3349433
    user3349433 about 9 years
    You can use multiple categories indeed, but at one time you are in one of them. You should know one volume is to one category, so you will have multiple volume values and it's user's choice to make different volume. You can just leave it alone.
  • Bista
    Bista about 8 years
    How to make sound come through both earphones and speaker on iPhone?
  • aBikis
    aBikis almost 7 years
    to set permanently: audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord‌​, with: AVAudioSessionCategoryOptions.defaultToSpeaker)
  • famfamfam
    famfamfam about 3 years
    hi, did this method. work with webrtc? Thanks