单击AVAudioplayer Swift Xcode中的按钮时自动更改文件(URL,路径)

时间:2018-01-07 00:42:28

标签: ios swift audio path

每个屏幕有3张图片,每张图片都有不同的声音文件“apple.wav”,“orange.wav”和点击后播放的“ball.wav”。

我有AVAudioPlayer代码,但我正在尝试简化它,因此我没有为每张照片playSoundOrange()playSoundofApple()分别设置一个功能。

尝试使用相同的功能,但在按钮操作中输入文件的名称。

按钮代码:

@IBAction func OrangeButton(_ sender: UIButton) {
    print("pressed OrangeButton")
    playSoundFile(orange.wav) //having it like this would be ideal. 
}

当前的func我正在尝试修改,所以我可以为每个按钮使用代码playSoundFile(orange.wav),只需将参数更改为(apple.wav)或(ball.wav)。

var player : AVAudioPlayer?
func playSoundFile(){
    let path = Bundle.main.path(forResource: "*orange*",   ofType:"*wav*")!
    let url = URL(fileURLWithPath: path)

    do {
        let sound = try AVAudioPlayer(contentsOf: url)
        self.player = sound
        sound.numberOfLoops = 0
        sound.prepareToPlay()
        sound.play()
    } catch {
        print("error loading file")
        // couldn't load file :(
    }
}

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

更新您的playSoundFile方法以获取声音名称:

func playSoundFile(_ soundName: String) {
    let url = Bundle.main.url(forResource: soundName, withExtension: "wav")!

    do {
        let sound = try AVAudioPlayer(contentsOf: url)
        self.player = sound
        sound.numberOfLoops = 0
        sound.prepareToPlay()
        sound.play()
    } catch {
        print("error loading file")
        // couldn't load file :(
    }
}

现在您只需要一个按钮动作即可用于所有三个按钮。

@IBAction func buttonPressed(_ sender: UIButton) {
}

将一个动作分配给所有三个按钮。

由于每个按钮都有一个插座,请将sender属性与每个插座进行比较,以确定要使用的文件名:

@IBAction func buttonPressed(_ sender: UIButton) {
    var soundName: String? = nil
    if sender == orangeButton {
        soundName = "orange"
    } else if sender == appleButton {
        soundName = "apple"
    } else if sender == ballButton {
        soundName = "ball"
    }

    if let soundName = soundName {
        playSoundFile(soundName)
    }
}

orangeButtonappleButtonballButton替换为您商店的实际名称。

答案 1 :(得分:0)

谢谢rmaddy解决它。只需将代码全部用于可能从中受益的其他新手。

import UIKit
import AVFoundation
import AudioToolbox

class ViewController: UIViewController {

    var player : AVAudioPlayer?

func playSoundFile(_ soundName: String) {
    let url = Bundle.main.url(forResource: soundName, withExtension: "wav")!

    do {
        let sound = try AVAudioPlayer(contentsOf: url)
        self.player = sound
        sound.numberOfLoops = 1
        sound.prepareToPlay()
        sound.play()
    } catch {
        print("error loading file")
        // couldn't load file :(
    }
}

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


    @IBAction func buttonPressed(_ sender: UIButton) {
        var soundName: String? = nil
        if sender.currentTitle == "john" {
            soundName = "john"
        }else if sender.currentTitle == "husain" {
                soundName = "husain"
        }else if sender.currentTitle == "tom" {
                    soundName = "tom"
             }
            if let soundName = soundName {
                playSoundFile(soundName)
            }
        }

}