快速创作和播放声音

 怎么找个名字这么费劲 发布于 2023-01-11 16:50

所以我想要做的是快速创建并播放声音,当我按下按钮时会播放,我知道如何在Objective-C中执行此操作,但有人知道如何在Swift中播放吗?

对于Objective-C,它会是这样的:

NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"mysoundname" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &mySound);

然后玩它我会做:

AudioServicesPlaySystemSound(Explosion);

有谁知道我怎么做到这一点?

7 个回答
  • 这与其他一些答案类似,但也许更多"Swifty":

    // Load "mysoundname.wav"
    if let soundURL = Bundle.main.url(forResource: "mysoundname", withExtension: "wav") {
        var mySound: SystemSoundID = 0
        AudioServicesCreateSystemSoundID(soundURL as CFURL, &mySound)
        // Play
        AudioServicesPlaySystemSound(mySound);
    }
    

    请注意,这是重现问题中代码效果的一个简单示例.你需要确保import AudioToolbox,加上这种代码的一般模式是在你的应用程序启动时加载你的声音,将它们保存在SystemSoundID某个地方的实例变量中,在整个应用程序中使用它们,然后AudioServicesDisposeSystemSoundID在你完成时调用跟他们.

    2023-01-11 16:51 回答
  • 以下是我添加到FlappySwift中的一些代码:

    import SpriteKit
    import AVFoundation
    
    class GameScene: SKScene {
    
        // Grab the path, make sure to add it to your project!
        var coinSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "coin", ofType: "wav")!)
        var audioPlayer = AVAudioPlayer()
    
        // Initial setup
        override func didMoveToView(view: SKView) {
            audioPlayer = AVAudioPlayer(contentsOfURL: coinSound, error: nil)
            audioPlayer.prepareToPlay()
        }
    
        // Trigger the sound effect when the player grabs the coin
        func didBeginContact(contact: SKPhysicsContact!) {
            audioPlayer.play()
        }
    
    }
    

    2023-01-11 16:51 回答
  • 这段代码适合我.使用Try和Catch for AVAudioPlayer

    import UIKit
    import AVFoundation
    class ViewController: UIViewController {
    
        //Make sure that sound file is present in your Project.
        var CatSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Meow-sounds.mp3", ofType: "mp3")!)
        var audioPlayer = AVAudioPlayer()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            do {
    
                audioPlayer = try AVAudioPlayer(contentsOfURL: CatSound)
                audioPlayer.prepareToPlay()
    
            } catch {
    
                print("Problem in getting File")
    
            }      
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        @IBAction func button1Action(sender: AnyObject) {
    
            audioPlayer.play()
        }
    }
    

    2023-01-11 16:53 回答
  • 使用class&AudioToolbox:

    import AudioToolbox
    
    class Sound {
    
        var soundEffect: SystemSoundID = 0
        init(name: String, type: String) {
            let path  = NSBundle.mainBundle().pathForResource(name, ofType: type)!
            let pathURL = NSURL(fileURLWithPath: path)
            AudioServicesCreateSystemSoundID(pathURL as CFURLRef, &soundEffect)
        }
    
        func play() {
            AudioServicesPlaySystemSound(soundEffect)
        }
    }
    

    用法:

    testSound = Sound(name: "test", type: "caf")
    testSound.play()
    

    2023-01-11 16:53 回答
  • SystemSoundID将从一个名为的文件创建一个Cha-Ching.aiff.

    import AudioToolbox
    
    let chaChingSound: SystemSoundID = createChaChingSound()
    
    class CashRegisterViewController: UIViewController {
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            AudioServicesPlaySystemSound(chaChingSound)
        }
    }
    
    func createChaChingSound() -> SystemSoundID {
        var soundID: SystemSoundID = 0
        let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "Cha-Ching", "aiff", nil)
        AudioServicesCreateSystemSoundID(soundURL, &soundID)
        CFRelease(soundURL)
        return soundID
    }
    

    2023-01-11 16:53 回答
  • import AVFoundation
    
    var audioPlayer = AVAudioPlayer()
    
    class GameScene: SKScene {
    
        override func didMoveToView(view: SKView) {
    
            let soundURL = NSBundle.mainBundle().URLForResource("04", withExtension: "mp3")
            audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
            audioPlayer.play()
        }
    }
    

    2023-01-11 16:53 回答
  • Handy Swift扩展:

    import AudioToolbox
    
    extension SystemSoundID {
        static func playFileNamed(fileName: String, withExtenstion fileExtension: String) {
            var sound: SystemSoundID = 0
            if let soundURL = NSBundle.mainBundle().URLForResource(fileName, withExtension: fileExtension) {
                AudioServicesCreateSystemSoundID(soundURL, &sound)
                AudioServicesPlaySystemSound(sound)
            }
        }
    }
    

    然后,从你的应用程序的任何地方(记得import AudioToolbox),你可以打电话

    SystemSoundID.playFileNamed("sound", withExtenstion: "mp3")
    

    玩"sound.mp3"

    2023-01-11 16:53 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有