Reputation: 183
I have a button in my app that I want to play a series of sound files at random. I've followed instructions from similar posts about this, but when I hit the play button, it plays a random sound file upon starting the emulator, but repeatedly. It doesn't shuffle through them on each button press. Below is the full code I have for my ViewController. Thanks for any help!
import UIKit
import AVFoundation
import AudioToolbox
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayerHyeeeehLong: AVAudioPlayer = AVAudioPlayer()
var audioPlayerHyeeeehShort: AVAudioPlayer = AVAudioPlayer()
var beWellAudioPlayer: AVAudioPlayer = AVAudioPlayer()
@IBOutlet weak var beWellButton: UIButton!
@IBOutlet weak var restoreMindBodyButton: UIButton!
@IBOutlet weak var spiritualCleanseButton: UIButton!
var randomIndex = 0
var soundFiles = ["sound1", "sound2", "sound3", "sound4", "sound5"]
override func viewDidLoad() {
super.viewDidLoad()
// play full spiritual cleanse
let hyeeeeh = Bundle.main.path(forResource: "hyeeeeh", ofType: "m4a")
do{
audioPlayerHyeeeehLong = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: hyeeeeh!))
}catch{
print(error)
}
// play restore mind body
let hyeeeehShort = Bundle.main.path(forResource: "hyeeeeh1", ofType: "m4a")
do{
audioPlayerHyeeeehShort = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: hyeeeehShort!))
}catch{
print(error)
}
// play be well
randomIndex = Int(arc4random_uniform(UInt32(soundFiles.count)))
let selectedFileName = soundFiles[randomIndex]
let beWell = Bundle.main.path(forResource: selectedFileName, ofType: "m4a")
do{
beWellAudioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: beWell!))
}catch{
print(error)
}
}
@IBAction func beWell(_ sender: Any) {
print("Be Well Button Pressed")
beWellAudioPlayer.play()
}
@IBAction func playShortHyeeh(_ sender: Any) {
audioPlayerHyeeeehShort.play()
}
@IBAction func playFullHyeeeh(_ sender: Any) {
audioPlayerHyeeeehLong.play()
}
}
EDIT: Below is the fixed code. Everything works! Thanks for your help, R.B Niranjan!
import UIKit
import AVFoundation
import AudioToolbox
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayerHyeeeehLong: AVAudioPlayer = AVAudioPlayer()
var audioPlayerHyeeeehShort: AVAudioPlayer = AVAudioPlayer()
var beWellAudioPlayer: AVAudioPlayer = AVAudioPlayer()
@IBOutlet weak var beWellButton: UIButton!
@IBOutlet weak var restoreMindBodyButton: UIButton!
@IBOutlet weak var spiritualCleanseButton: UIButton!
var randomIndex = 0
var soundFiles = ["sound1", "sound2", "sound3", "sound4", "sound5"]
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func beWell(_ sender: Any) {
randomIndex = Int(arc4random_uniform(UInt32(soundFiles.count)))
let selectedFileName = soundFiles[randomIndex]
let beWell = Bundle.main.path(forResource: selectedFileName, ofType: "m4a")
do{
beWellAudioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: beWell!))
}catch{
print(error)
}
if beWellAudioPlayer.isPlaying{
beWellAudioPlayer.pause()
}
beWellAudioPlayer.currentTime = 0
beWellAudioPlayer.play()
}
@IBAction func playShortHyeeh(_ sender: Any) {
let hyeeeehShort = Bundle.main.path(forResource: "hyeeeeh1", ofType: "m4a")
do{
audioPlayerHyeeeehShort = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: hyeeeehShort!))
}catch{
print(error)
}
if audioPlayerHyeeeehShort.isPlaying{
audioPlayerHyeeeehShort.pause()
}
audioPlayerHyeeeehShort.currentTime = 0
audioPlayerHyeeeehShort.play()
}
@IBAction func playFullHyeeeh(_ sender: Any) {
let hyeeeeh = Bundle.main.path(forResource: "hyeeeeh", ofType: "m4a")
do{
audioPlayerHyeeeehLong = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: hyeeeeh!))
}catch{
print(error)
}
if audioPlayerHyeeeehLong.isPlaying{
audioPlayerHyeeeehLong.pause()
}
audioPlayerHyeeeehLong.currentTime = 0
audioPlayerHyeeeehLong.play()
}
}
Upvotes: 0
Views: 699
Reputation: 628
Selecting a random audio file and playing should be done on a button click.
@IBAction func beWell(_ sender: Any) {
print("Be Well Button Pressed")
randomIndex = Int(arc4random_uniform(UInt32(soundFiles.count)))
let selectedFileName = soundFiles[randomIndex]
let beWell = Bundle.main.path(forResource: selectedFileName, ofType: "m4a")
do{
beWellAudioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: beWell!))
}catch{
print(error)
}
beWellAudioPlayer.play()
}
Upvotes: 2