在iOS中按下特定按钮时播放特定声音 [英] Play specific sound when specific button is pressed in iOS

查看:114
本文介绍了在iOS中按下特定按钮时播放特定声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的初学者,我试图制作一个简单的应用程序,当按下特定按钮时,该按钮会播放特定的声音,并带有两个分段的选项选项(如下图所示).我在YouTube,Stack和其他网站上查阅了很多教程,但它们似乎都给我带来很多错误,或者让我太难理解(因为我没有经验).

I'm a beginner in iOS development and I am trying to make a simple app which plays specific sounds when a specific button is pressed with two segmented control for options (as shown in the image below). I have looked up many tutorials on YouTube, stack and other websites, but all of them seem to either give me a lot of errors or are too hard for me too understand (as I am too unexperienced).

我进行了一些尝试,但这仅适用于1个按钮和1个声音.

Some of my attempts, but this work for 1 button and 1 sound only.

import UIKit
import AVFoundation

class ViewController: UIViewController {
    // make sure to add this sound to your project
    var pianoSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C", ofType: "m4a"))
    var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        audioPlayer = AVAudioPlayer(contentsOfURL: pianoSound, error: nil)
        audioPlayer.prepareToPlay()
    }

    @IBAction func PianoC(sender: AnyObject) {
        audioPlayer.play()
    }
}

请,有人可以告诉我如何制作此应用程序,如下图所示吗?

http://imgur.com/tWgCDWD

必需:有三个按钮(猫,狗,鸟),每个按钮都有自己的声音.和第一个分段的(声音)用于启用或禁用声音,例如.当用户选择关闭声音"选项时,即使单击该按钮也不会发出声音,并且第二个分段用于启用或禁用背景音乐.

Please, can someone show me how to make this app, as shown in the image below?

http://imgur.com/tWgCDWD

Required: there are three buttons ( cat, dog, bird ) , every button have own sound. and the first segmented ( sound ) for enable or disable the sound, eg. when user choose sound off option there will be no sound even the button clicked, and the second segmented for enable or disable the music on the background .

推荐答案

import UIKit
import AVFoundation

class ViewController: UIViewController {

    @IBOutlet weak var playSoundSwitch: UISegmentedControl!

    var backgroundMusicPlayer: AVAudioPlayer?

    var player:AVAudioPlayer = AVAudioPlayer()

    @discardableResult func playSound(named soundName: String) -> AVAudioPlayer {


        let audioPath = Bundle.main.path(forResource: soundName, ofType: "wav")
        player = try! AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
        player.play()
        return player
    }


    @IBAction func catButtonPressed(_ sender: Any) {

        if playSoundSwitch.selectedSegmentIndex == 0 {
            playSound(named: "catSound")
        }


    }

    @IBAction func dogButtonPressed(_ sender: Any) {
        if playSoundSwitch.selectedSegmentIndex == 0 {
            playSound(named: "dogSound")
        }
    }

    @IBAction func birdButtonPressed(_ sender: Any) {

        if playSoundSwitch.selectedSegmentIndex == 0 {
            playSound(named: "birdSound")
            print("bird sound")
        }
    }

    @IBAction func playBackgroundMusicSwitchChanged(_ sender: Any) {


        if (sender as AnyObject).selectedSegmentIndex == 0 {
            backgroundMusicPlayer = playSound(named: "backgroundSound")
        } else {
            backgroundMusicPlayer?.stop()
            backgroundMusicPlayer = nil
        }
    }

}

这篇关于在iOS中按下特定按钮时播放特定声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆