0

Hello I tried the various solutions to similar questions but couldnt get my code to work. I have the following function that I call in my app:

func PlaySound (WhenToPlaySound:String) {

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
    AVAudioSession.sharedInstance().setActive(true, error: nil)

    if WhenToPlaySound == "BeginningOfRound" {

        if UIApplication.sharedApplication().applicationState == UIApplicationState.Background {

            soundnotification.soundName = "BoxingBellStart.wav"

            UIApplication.sharedApplication().scheduleLocalNotification(soundnotification)

            println("timer is done in background mode")

        } else {

            // Load Sound
            soundlocation = NSBundle.mainBundle().URLForResource("BoxingBellStart", withExtension: "wav")!
            player = AVAudioPlayer(contentsOfURL: soundlocation, error: &Error)
            player.volume = 1.0

            // Play Sound
            player.play()

            println("timer is done in active mode")

        }

    } else {

        if UIApplication.sharedApplication().applicationState == UIApplicationState.Background {

            soundnotification.soundName = "Boxing.wav"

            UIApplication.sharedApplication().scheduleLocalNotification(soundnotification)

            println("timer is done in background mode")

        } else {

            // Load Sound
            soundlocation = NSBundle.mainBundle().URLForResource("Boxing", withExtension: "wav")!
            player = AVAudioPlayer(contentsOfURL: soundlocation, error: &Error)
            player.volume = 1.0

            // Play Sound
            player.play()

            println("timer is done in active mode")

        }

    }
}

Mostly it works except two things:

1. I can't seem to override the speaker volume. I want the system volume to be full volume before I play my sound.

  1. The LocalNotifications that are set to activate when the app is in background mode only pay when the device isn't muted.

To address the first problem I wanted to the following but didnt know how to use it:

AVAudioSession.sharedInstance().overrideOutputAudioPort(<#portOverride: AVAudioSessionPortOverride#>, error: <#NSErrorPointer#>) 

Thanks in advance, Ace

Ace Green
  • 381
  • 5
  • 29

1 Answers1

1

overrideOutputAudioPort affects the audio routing, not the volume.

When you say that you want to "override the speaker volume", I assume that you mean you want to control the system output volume from you app code. This is not possible as Apple believes that output volume should remain in the control of the user at all times.

AVAudioPlayer's volume property sets the volume relative to the system output level. It defaults to 1.0 (player volume == system volume). You can't turn it up higher, spinal-tap style, to 1.1...

See also my answer here ... if you want to take control of the system volume, you will need the user interface provided by MPVolumeView.

Similarly regarding your notifications - if the user has muted the device, your app won't be able to ignore that.

update
regarding notifications, it isn't as straightforward as I suggested. It might work if you set the AVAudioSession category to AVAudioSessionCategoryPlayback (and read the apple docs on this setting).

When using this category, your app audio continues with the Silent switch set to silent or when the screen locks

You may also need to add 'audio' to UIBackgroundModes in your info.plist.

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
  • regarding point 2. I did notice another app achieving local notification sounds even if the device is in silent. If the volume volume is muted then it doesn't work but with volume and on silent, the notification sound is played. – Ace Green Mar 06 '15 at 17:15
  • @AceGreen - see my update. Regarding point 1, you can't change the system volume without user interaction (e.g. via MPVolumeView). – foundry Mar 06 '15 at 18:08
  • Ok I discarded point 1. Point 2 is possible as I see an app called RoundTimer doing it. – Ace Green Mar 06 '15 at 21:20
  • With my current setup: In active mode, both the AVAudio and local notifications play sound in silent and normal mode. In background mode, the AVAudio plays in silent and normal mode, while the LocalNotification only plays in normal mode. – Ace Green Mar 06 '15 at 21:21
  • @foundry, I've overide to the speaker , Is that session is valid for playing via youtube.com through my web view ? my session works fine with AVPlayer, but once I loaded any URL for audio then it is rerouting to the default route (as per the device standards) – Kumar KL Mar 03 '16 at 06:17