如何编写代码来快速停止多个音频文件?
Posted
技术标签:
【中文标题】如何编写代码来快速停止多个音频文件?【英文标题】:How to write code to stop multiple audio files swift? 【发布时间】:2016-06-14 05:43:25 【问题描述】:早安
如何编写代码来停止这段代码中的多个音频文件
我找了,没找到有用的,请帮帮我
import UIKit
import AVFoundation
class ViewController: UIViewController
let soundFilenames = ["001" , "002" , "003" , "004" , "005" , "006" , "007" , "008"]
var Audios = [AVAudioPlayer]()
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for sound in soundFilenames
do
let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
Audios.append(audioPlayer)
catch
Audios.append(AVAudioPlayer())
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func buttun001(sender: UIButton)
let audioplayer = Audios [sender.tag]
audioplayer.play()
【问题讨论】:
【参考方案1】:使用正常循环,检查是否正在播放并停止播放
func stopAudios()
for audioPLayer in Audios
if audioPLayer.playing
audioPLayer.stop()
别忘了准备好你的 AVAudioPlayer
audioPlayer.prepareToPlay()
【讨论】:
以上是关于如何编写代码来快速停止多个音频文件?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 python 脚本停止/关闭音频文件(mp3/.wav)