如何在Swift中停止/取消playSoundFileNamed? [英] How to stop/cancel playSoundFileNamed in Swift?

查看:119
本文介绍了如何在Swift中停止/取消playSoundFileNamed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个按钮,按下该按钮时-声音会播放,再次按下时-声音会停止. 我正在使用playSoundFileNamed: runAction: withKey:removeActionForKey,但是它不起作用.

I'm trying to make a button, which is when pushed - the sound is played, and when pushed again - the sound would stop. I'm using playSoundFileNamed: runAction: withKey: and removeActionForKey, but it doesn't work.

我的另一个问题是,是否有一种不仅可以停止声音,而且可以暂停声音的方法(这样它就可以从暂停的那一部分开始,而不是从头开始). 我在堆栈溢出时也看到过类似的话题,但是还没有找到关于我的问题的答案.预先谢谢你.

The other question I have is if there is a way not only to stop the sound, but also to pause it (so that it will start from the same part it was paused, NOT from the beginning). I've seen a similar topic at stack overflow, but yet didn't find an answer on my question yet. Thank you in advance.

import SpriteKit
import AVFoundation

var firesoundon = false

private var backgroundMusicPlayer: AVAudioPlayer!

class GameScene2: SKScene {

override func didMoveToView(view: SKView) {

    setUpScenery()    
}

private func setUpScenery() {


    //BACKGROUND
    let background = SKSpriteNode(imageNamed: backgroundImage, normalMapped: true)
    addChild(background)


    //FIRE BUTTON

    var firePath = UIBezierPath() 
    firePath.moveToPoint(CGPointMake(0, 0)) 
    firePath.addCurveToPoint(CGPointMake(115, 215), controlPoint1: CGPointMake(5, 170), controlPoint2: CGPointMake(90, 190)) ....

//^我在这里只是减少了很多路径-按钮被绘制,并且是可推动/可操作的.

//^I'm just cutting down a lot of path here - the button is drawn and it's pushable/workable.

    let fireNode = SKShapeNode(path: firePath.CGPath, centered: false)
    fireNode.alpha = 0.2

    fireNode.position = CGPointMake(size.width/2, 0)
    fireNode.name = "fireNode"

        self.addChild(fireNode)
}

//所以,这对我来说是一个主要的遗憾:

//So, this is a main bummer for me:

func fireturnonoff () {

    if firesoundon == false {

        var playguitar = SKAction.playSoundFileNamed("guitar.wav", waitForCompletion: true)
        runAction(playguitar, withKey: "soundsison")

        firesoundon = true
        println("firesoundon is == \(firesoundon)")

    }

    else if firesoundon == true {

        removeActionForKey("soundsison")
        println("REMOVED")

        firesoundon = false
        println("firesoundon is == \(firesoundon)")

    }

}


override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let touch = touches.first as? UITouch

        let touchLocation = touch!.locationInNode(self)
        let node: SKNode = nodeAtPoint(touchLocation)

        if node.name == "fireNode" {

            fireturnonoff()

        }
}
}

推荐答案

Swift中为SKAction添加了新功能:

New functionality has been added to Swift for SKActions:

class func stop() -> SKAction

它创建一个告诉音频节点停止播放的动作. 此操作只能在SKAudioNode对象上执行.音频停止,并且如果重新启动,则从头开始.此操作不可逆.

It creates an action that tells an audio node to stop playback. This action may only be executed on an SKAudioNode object. The audio is stopped, and if restarted, begins at the beginning. This action is not reversible.

很遗憾,仅在iOS 9.0和更高版本中可用.

Unfortunately, available only in iOS 9.0 and later.

这篇关于如何在Swift中停止/取消playSoundFileNamed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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