jeudi 11 août 2016

Trying to press one button then play audios in sequence or random

I am trying to press one button and the audio file plays one after another.

Here is my code below. I press play button and both sounds play at the same time. I am trying to press play button and the sound starts and stop then the other sound file starts and stops. One button pressed for 2 sound files to press is my goal. Thank you

I am trying this in a loop but it is not working.

import UIKit import AVFoundation

class ViewController: UIViewController {

@IBOutlet weak var sliderValue: UISlider!

var player:AVAudioPlayer = AVAudioPlayer()
var player1:AVAudioPlayer = AVAudioPlayer()

@IBAction func play(_ sender: AnyObject) {
       player.play()
       player1.play()
}

@IBAction func pause(_ sender: AnyObject) {
    player.pause()
}
@IBAction func stop(_ sender: AnyObject) {
    player.stop()
    player.currentTime = 0 
}
@IBAction func sliderChanged(_ sender: AnyObject) {
    player.volume = sliderValue.value 
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let audioPath =  Bundle.main.path(forResource: "sound1", ofType: "mp3")!
    let audioPath1 =  Bundle.main.path(forResource: "sound2", ofType: "mp3")!
    do {   
        try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath))
        try player1 = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath1))

    } catch{
        //Process Error here
    }      
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}




Aucun commentaire:

Enregistrer un commentaire