3

I am trying to create a slider that controls volume of 4 audio files.

Here is my code for the audio:

var ButtonAudioURL  = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("audio1", ofType: "mp3")!)

var firstplayer     = AVAudioPlayer()    
var ButtonAudioURL2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("audio2", ofType: "mp3")!) 

var secondplayer   = AVAudioPlayer()    
var ButtonAudioURL3 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("audio3", ofType: "mp3")!)

var thirdplayer = AVAudioPlayer()    
var ButtonAudioURL4 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("audio4", ofType: "mp3")!)

var forthplayer = AVAudioPlayer()

override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    firstplayer  = AVAudioPlayer(contentsOfURL: ButtonAudioURL, error: nil)        
    secondplayer = AVAudioPlayer(contentsOfURL: ButtonAudioURL2, error: nil)     
    thirdplayer  = AVAudioPlayer(contentsOfURL: ButtonAudioURL3, error: nil)       
    forthplayer  = AVAudioPlayer(contentsOfURL: ButtonAudioURL4, error: nil)

I have the audio to play when each button is touched. But I just need to get a slider to control the volume of each audio file from each button.

How should i do this? I have looked at MPVolumeView class reference on developer.apple, but I am still confused and in the developer.apple reference for MPVolumeView, it talks about AirPlay, can I disable that?

I just need a slider to control the volume.

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
foxcowz
  • 109
  • 1
  • 2
  • 4

3 Answers3

3

You're close!

Drag in a slider into your storyboard, and create an IB outlet and IB action for it.

In the IB action code, use firstPlayer.volume = sliderOutlet.value

Since both range from 0 to 1 as default, they are on the same scale and so can be used like this without scaling. You may need to declare the AVAudioPlayers more globally and have a way of seeing which button was pressed (e.g. a variable that stores the name of the button pressed). That way, you change the correct volume using 4 if statements and don't end up changing the volume for a player that has not been initialized yet.

Let me know if this works!

3

I am using Xcode 6. I am building a similar example and I dragged from the slider in the storyboard to my viewController and created an Outlet

@IBOutlet weak var volumeControl: UISlider!

after that I created an action

@IBAction func adjustVolume(sender: AnyObject) {
    if audioPlayer != nil {
        audioPlayer?.volume = volumeControl.value
    }
}

and it is working for me. You can create 4 sliders and assign them to each instance of AudioPlayer you created. In this link you can find an example of Playing an audio on iOS8 using AVAudioPlayer http://www.techotopia.com/index.php/Playing_Audio_on_iOS_8_using_AVAudioPlayer

Jorge Luis Jiménez
  • 1,203
  • 13
  • 20
3

If you use Swift 2 Try with this:

@IBOutlet weak var volumen: UISlider!
var reproductor = AVAudioPlayer()

after:

    @IBAction func sliderValueChanged(sender: UISlider) {

    let selectedValue = Float(sender.value)
    reproductor.volume = selectedValue

}

and try your slider volume during the playing sound. That is all. I've helped.

gandhi Mena
  • 2,115
  • 1
  • 19
  • 20