如何玩同样的声音与AVAudioPlayer重叠? [英] How to play the same sound overlapping with AVAudioPlayer?

查看:102
本文介绍了如何玩同样的声音与AVAudioPlayer重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code起着当按钮被点击的声音,但取消了previous如果再次pssed $ P $。我不希望这种事情发生我想同样的声音重叠时反复pressed。我相信这可能是由于使用相同的AVAudioPlayer,因为我已经看过了互联网上,但我是新来的迅速,想知道如何创建一个新的AVAudioPlayer每次方法运行,因此声音重叠。

  FUNC playSound(音:字符串){
    //设置声音文件的名称和放大器;延期    让和声= NSURL(fileURLWithPath:NSBundle.mainBundle()pathForResource(音,ofType:MP3)!)
    做{
        // preperation
        尝试AVAudioSession.sharedInstance()。setCategory(AVAudioSessionCategoryPlayback)
    }赶上_ {
    }
    做{
        尝试AVAudioSession.sharedInstance()。SETACTIVE(真)
    }
    赶上_ {
    }
    //播放声音
    VAR错误:NSError?
    做{
        audioPlayer =尝试AVAudioPlayer(contentsOfURL:和声)
    }赶上让ERROR1为NSError {
        错误= ERROR1
    }   audioPlayer。prepareToPlay()
  audioPlayer.play()}


解决方案

要两个声音同时播放与AVAudioPlayer你只需要使用不同的球员每种声音。

在我的例子中,我声明了两个球员, playerBoom playerCrash 在的viewController了,我中号填充它们拥有完善的通过功能的发挥,然后触发播放一次:

 进口AVFoundation一流的ViewController:UIViewController的{    VAR playerBoom:AVAudioPlayer?
    VAR playerCrash:AVAudioPlayer?    覆盖FUNC viewDidLoad中(){
        super.viewDidLoad()        playerBoom = preparePlayerForSound(命名为:Sound1例子)
        playerCrash = preparePlayerForSound(命名为:SOUND2)        playerBoom?prepareToPlay()
        playerCrash?prepareToPlay()
        playerBoom?.play()
        playerCrash?.play()    }    FUNC preparePlayerForSound(命名声:字符串) - GT; AVAudioPlayer? {
        做{
            如果让和声= NSBundle.mainBundle()pathForResource(音,ofType:MP3){
                尝试AVAudioSession.sharedInstance()。setCategory(AVAudioSessionCategoryPlayback)
                尝试AVAudioSession.sharedInstance()。SETACTIVE(真)
                返回尝试AVAudioPlayer(contentsOfURL:NSURL(fileURLWithPath:和声))
            }其他{
                打印(文件\\(音).MP3不可用)
            }
        }赶上让误差NSError {
            打印(错误)
        }
        零回报
    }}

这工作得很好,但如果你有很多的声音打IMO是不适合的。这对只有几的人一个完全有效的解决方案,但

这个例子是两种不同的声音,但当然的想法是完全两个同文的声音是一样的。

This code plays the sound when the button is tapped but cancels the previous if it is pressed again. I do not want this to happen I want the same sound to overlap when repeatedly pressed. I believe it might be due to using the same AVAudioPlayer as I have looked on the internet but I am new to swift and want to know how to create a new AVAudioPlayer everytime the method runs so the sounds overlap.

   func playSound(sound:String){


    // Set the sound file name & extension

    let soundPath = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
    do {
        //Preperation
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    } catch _{
    }
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    }
    catch _ {
    }
    //Play the sound
    var error:NSError?
    do{
        audioPlayer = try AVAudioPlayer(contentsOfURL: soundPath)
    }catch let error1 as NSError {
        error = error1
    }

   audioPlayer.prepareToPlay()
  audioPlayer.play()

}

解决方案

To play two sounds simultaneously with AVAudioPlayer you just have to use a different player for each sound.

In my example I've declared two players, playerBoom and playerCrash, in the Viewcontroller, and I'm populating them with a sound to play via a function, then trigger the play at once:

import AVFoundation

class ViewController: UIViewController {

    var playerBoom:AVAudioPlayer?
    var playerCrash:AVAudioPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        playerBoom = preparePlayerForSound(named: "sound1")
        playerCrash = preparePlayerForSound(named: "sound2")

        playerBoom?.prepareToPlay()
        playerCrash?.prepareToPlay()
        playerBoom?.play()
        playerCrash?.play()

    }

    func preparePlayerForSound(named sound: String) -> AVAudioPlayer? {
        do {
            if let soundPath = NSBundle.mainBundle().pathForResource(sound, ofType: "mp3") {
                try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                try AVAudioSession.sharedInstance().setActive(true)
                return try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: soundPath))
            } else {
                print("The file '\(sound).mp3' is not available")
            }
        } catch let error as NSError {
            print(error)
        }
        return nil
    }

}

It works very well but IMO is not suitable if you have many sounds to play. It's a perfectly valid solution for just a few ones, though.

This example is with two different sounds but of course the idea is exactly the same for two identic sounds.

这篇关于如何玩同样的声音与AVAudioPlayer重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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