Swift,如何通过按下按钮一次又一次地播放声音

Posted

技术标签:

【中文标题】Swift,如何通过按下按钮一次又一次地播放声音【英文标题】:Swift, how to play sound again and again by pressing a button 【发布时间】:2014-10-02 04:00:23 【问题描述】:

我正在制作我的第一个钢琴应用程序,但遇到了麻烦。

如何在按下按钮时反复播放声音?

例如,当我按下按钮时,我可以播放 C4 声音。 但是当我想再次播放C4声音时,我必须等到声音完全消失。 我想按按钮多次播放 C4 声音。

这是我的“view controller.swift”

提前致谢。

import UIKit
import AVFoundation

class ViewController: UIViewController 
// make sure to add this sound to your project

var pianoSoundC4 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoC4", ofType: "m4a")!)
var audioPlayerC4 = AVAudioPlayer()

var pianoSoundCS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoC#", ofType: "m4a")!)
var audioPlayerCS = AVAudioPlayer()

var pianoSoundD = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoD", ofType: "m4a")!)
var audioPlayerD = AVAudioPlayer()

var pianoSoundDS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoD#", ofType: "m4a")!)
var audioPlayerDS = AVAudioPlayer()

var pianoSoundE = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoE", ofType: "m4a")!)
var audioPlayerE = AVAudioPlayer()

var pianoSoundF = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoF", ofType: "m4a")!)
var audioPlayerF = AVAudioPlayer()

var pianoSoundFS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoF#", ofType: "m4a")!)
var audioPlayerFS = AVAudioPlayer()

var pianoSoundG = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoG", ofType: "m4a")!)
var audioPlayerG = AVAudioPlayer()

var pianoSoundGS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoG#", ofType: "m4a")!)
var audioPlayerGS = AVAudioPlayer()

var pianoSoundA = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoA", ofType: "m4a")!)
var audioPlayerA = AVAudioPlayer()

var pianoSoundAS = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoA#", ofType: "m4a")!)
var audioPlayerAS = AVAudioPlayer()

var pianoSoundB = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoB", ofType: "m4a")!)
var audioPlayerB = AVAudioPlayer()

var pianoSoundC5 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("PianoC5", ofType: "m4a")!)
var audioPlayerC5 = AVAudioPlayer()

override func viewDidLoad() 
    super.viewDidLoad()

    audioPlayerC4 = AVAudioPlayer(contentsOfURL: pianoSoundC4, error: nil)
    audioPlayerC4.prepareToPlay()

    audioPlayerCS = AVAudioPlayer(contentsOfURL: pianoSoundCS, error: nil)
    audioPlayerCS.prepareToPlay()

    audioPlayerD = AVAudioPlayer(contentsOfURL: pianoSoundD, error: nil)
    audioPlayerD.prepareToPlay()

    audioPlayerDS = AVAudioPlayer(contentsOfURL: pianoSoundDS, error: nil)
    audioPlayerDS.prepareToPlay()

    audioPlayerE = AVAudioPlayer(contentsOfURL: pianoSoundE, error: nil)
    audioPlayerE.prepareToPlay()

    audioPlayerF = AVAudioPlayer(contentsOfURL: pianoSoundF, error: nil)
    audioPlayerF.prepareToPlay()

    audioPlayerFS = AVAudioPlayer(contentsOfURL: pianoSoundFS, error: nil)
    audioPlayerFS.prepareToPlay()

    audioPlayerG = AVAudioPlayer(contentsOfURL: pianoSoundG, error: nil)
    audioPlayerG.prepareToPlay()

    audioPlayerGS = AVAudioPlayer(contentsOfURL: pianoSoundGS, error: nil)
    audioPlayerGS.prepareToPlay()

    audioPlayerA = AVAudioPlayer(contentsOfURL: pianoSoundA, error: nil)
    audioPlayerA.prepareToPlay()

    audioPlayerAS = AVAudioPlayer(contentsOfURL: pianoSoundAS, error: nil)
    audioPlayerAS.prepareToPlay()

    audioPlayerB = AVAudioPlayer(contentsOfURL: pianoSoundB, error: nil)
    audioPlayerB.prepareToPlay()

    audioPlayerC5 = AVAudioPlayer(contentsOfURL: pianoSoundC5, error: nil)
    audioPlayerC5.prepareToPlay()


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()


@IBAction func C4(sender: UIButton) 
    audioPlayerC4.play()


@IBAction func CS(sender: UIButton) 
    audioPlayerCS.play()


@IBAction func D(sender: UIButton) 
    audioPlayerD.play()


@IBAction func DS(sender: UIButton) 
    audioPlayerDS.play()


@IBAction func E(sender: UIButton) 
audioPlayerE.play()


@IBAction func F(sender: UIButton) 
audioPlayerF.play()


@IBAction func FS(sender: UIButton) 
audioPlayerFS.play()


@IBAction func G(sender: UIButton) 
audioPlayerG.play()


@IBAction func GS(sender: UIButton) 
audioPlayerGS.play()


@IBAction func A(sender: UIButton) 
audioPlayerA.play()


@IBAction func AS(sender: UIButton) 
audioPlayerAS.play()


@IBAction func B(sender: UIButton) 
audioPlayerB.play()


@IBAction func C5(sender: UIButton) 
audioPlayerC5.play()

【问题讨论】:

【参考方案1】:

您必须先停止声音,然后才能再次播放。这里有一个很好的讨论:

Swift - Have an audio repeat, but it plays in intervals?

【讨论】:

感谢您的评论。我不知道我必须停止声音。下面的代码有效。非常感谢! @IBAction func C4(sender: UIButton) audioPlayerC4.stop() audioPlayerC4.currentTime = 0; audioPlayerC4.play()

以上是关于Swift,如何通过按下按钮一次又一次地播放声音的主要内容,如果未能解决你的问题,请参考以下文章

如果在消息框中按下“否”,程序会一次又一次地返回消息框?

我的音乐播放器应用程序一次又一次地崩溃

Swift:每次重新加载时都会一次又一次地添加表格视图单元格内容?

一次又一次地在新的相同标签中打开一个网址

即使一次又一次地刷新页面,如何在车把中永久设置所选选项?

连续播放声音 onTouch()