当我的应用程序检测到带有ARKit的特定图像时,如何播放特定的声音?

时间:2019-01-20 11:46:46

标签: ios swift audio avfoundation arkit

在我的应用程序中,我使用ARKit 1.5函数识别图像,当他识别特定图像时,他在ViewController中显示信息。我已经设置了一个按钮来播放图像描述,但此刻是通用声音。

我尝试将音频设置为String,但是它不起作用,以及尝试添加一个if if else来根据所识别的图像类型选择音频。

这是我的代码,除以ViewController。

ViewController:

struct ImageInformation {
    let name: String
    let description: String
    let image: UIImage
    let audio: AVAudioPlayer
}

let images = ["Mona_Lisa" : ImageInformation(name: "Mona Lisa", description: "The Mona Lisa is a half-length portrait painting by the Italian Renaissance artist Leonardo da Vinci that has been described as 'the best known, the most visited, the most written about, the most sung about, the most parodied work of art in the world.", image: UIImage(named: "Mona_Lisa")!),

              "image1" : ImageInformation(name: "Hi1", description: "Hello1", image: UIImage(named: "image1")!),

              "image2" : ImageInformation(name: "Hi2", description: "hello2", image: UIImage(named: "image2")!),

              "Raffaello_Angelo" : ImageInformation(name: "image3", description: "hello3", image: UIImage(named: "image3")!),]

InformationViewController:

    if let actualImageInformation = imageInformation {
        self.nameLabel.text = actualImageInformation.name
        self.imageView.image = actualImageInformation.image
        self.descriptionText.text = actualImageInformation.description

    }

    func listenAudio(){

    guard let url = Bundle.main.url(forResource: "temperatures-rising", withExtension: "mp3") else { return }

    do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
        try AVAudioSession.sharedInstance().setActive(true)

        self.player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)

        guard let player = self.player else { return }

        player.play()

        self.audioButton.setTitle("Stop", for: UIControl.State.normal)
        self.player!.play()


    } catch let error {
        print(error.localizedDescription)
    }

}

0 个答案:

没有答案