【问题标题】: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()

    }



   }

【问题讨论】:

    标签: iphone xcode swift audio audio-streaming


    【解决方案1】:

    使用正常循环,检查是否正在播放并停止播放

    func stopAudios(){
        for audioPLayer in Audios{
            if audioPLayer.playing{
                audioPLayer.stop()
            }
        }
    }
    

    别忘了准备好你的 AVAudioPlayer

    audioPlayer.prepareToPlay()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多