如何在 Xcode11 中的 Swift 中随机播放数组中的音频文件?
Posted
技术标签:
【中文标题】如何在 Xcode11 中的 Swift 中随机播放数组中的音频文件?【英文标题】:How do I play audio files from an array at random in Swift in Xcode11? 【发布时间】:2019-10-02 20:51:28 【问题描述】:我的应用中有一个按钮,我想随机播放一系列声音文件。我已经按照类似帖子的说明进行操作,但是当我点击播放按钮时,它会在启动模拟器时播放随机声音文件,但会重复播放。它不会在每次按下按钮时随机播放它们。下面是我的 ViewController 的完整代码。感谢您的帮助!
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()
编辑: 下面是固定代码。一切正常!感谢您的帮助,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()
【问题讨论】:
【参考方案1】:选择一个随机的音频文件并播放应该通过单击按钮来完成。
@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()
【讨论】:
这成功了!非常感谢!这是我第一次使用 Swift 编码并制作 ios 应用程序,所以我对布局还是很陌生。再次感谢!以上是关于如何在 Xcode11 中的 Swift 中随机播放数组中的音频文件?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Xcode 中将 Swift 版本从 5 更改为 4?
删除 List Swift UI Xcode 11 中的空行
如何从 appdelegate xcode 11 swift 5 呈现视图控制器