在斯威夫特游乐场播放声音

时间:2023-01-24 08:35:41

How can I play a sound file with AVFoundation in a Swift playground? Ideally, the file would be in a URL, not in the local drive, but that's not important.

如何在Swift游乐场中播放AVFoundation的声音文件?理想情况下,文件将位于URL中,而不是本地驱动器中,但这并不重要。

2 个解决方案

#1


2  

I'm fumbling around in the dark on this one myself, but I think it may be due to some limitation of swift playgrounds. Consider this code:

我自己在黑暗中摸索着,但我认为这可能是由于游乐场的一些限制。考虑以下代码:

#!/usr/bin/swift

import Cocoa
import AVFoundation

var error: NSError?

println("Hello, Audio!")
var url = NSURL(fileURLWithPath: "/Users/somebody/myfile.mid") // Change to a local midi file
var midi = AVMIDIPlayer(contentsOfURL: url, soundBankURL: nil, error: &error)
if midi == nil {
    if let e = error {
        println("AVMIDIPlayer failed: " + e.localizedDescription)
    }
}
midi.play(nil)
while midi.playing {
    // Spin (yeah, that's bad!)
}

If we run this swift script in the terminal, it works fine and plays the midi file, but if you run the code in a playground, you get

如果我们在终端中运行这个快速脚本,它工作正常并播放midi文件,但是如果你在游乐场中运行代码,你会得到

AVMIDIPlayer failed: The operation couldn’t be completed. (com.apple.coreaudio.avfaudio error -1.)

AVMIDIPlayer失败:无法完成操作。 (com.apple.coreaudio.avfaudio错误-1。)

On the positive side, being able to run it in a terminal shows that the code does work.

从积极的方面来说,能够在终端中运行它表明代码确实有效。

#2


1  

This works on a project I haven't tried on Playground. First drag your sound to your project and choose to copy for your destination if needed and check "add to target" to your app.

这适用于我在Playground上没试过的项目。首先将声音拖到项目中,然后根据需要选择复制目的地并选中“添加到目标”到您的应用。

import Cocoa
import AVFoundation


var beepSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("beep", ofType: "aif")!)
func initBeep(){
    beepPlayer = AVAudioPlayer(contentsOfURL: beepSound, error: nil)
    beepPlayer.prepareToPlay()
}

func playBeepSound(){
    beepPlayer.play()
}
func applicationDidFinishLaunching(aNotification: NSNotification?) { 

    initBeep() 
}
@IBAction func btnPlayBeepSound(sender: AnyObject) {
   playBeepSound()
}

#1


2  

I'm fumbling around in the dark on this one myself, but I think it may be due to some limitation of swift playgrounds. Consider this code:

我自己在黑暗中摸索着,但我认为这可能是由于游乐场的一些限制。考虑以下代码:

#!/usr/bin/swift

import Cocoa
import AVFoundation

var error: NSError?

println("Hello, Audio!")
var url = NSURL(fileURLWithPath: "/Users/somebody/myfile.mid") // Change to a local midi file
var midi = AVMIDIPlayer(contentsOfURL: url, soundBankURL: nil, error: &error)
if midi == nil {
    if let e = error {
        println("AVMIDIPlayer failed: " + e.localizedDescription)
    }
}
midi.play(nil)
while midi.playing {
    // Spin (yeah, that's bad!)
}

If we run this swift script in the terminal, it works fine and plays the midi file, but if you run the code in a playground, you get

如果我们在终端中运行这个快速脚本,它工作正常并播放midi文件,但是如果你在游乐场中运行代码,你会得到

AVMIDIPlayer failed: The operation couldn’t be completed. (com.apple.coreaudio.avfaudio error -1.)

AVMIDIPlayer失败:无法完成操作。 (com.apple.coreaudio.avfaudio错误-1。)

On the positive side, being able to run it in a terminal shows that the code does work.

从积极的方面来说,能够在终端中运行它表明代码确实有效。

#2


1  

This works on a project I haven't tried on Playground. First drag your sound to your project and choose to copy for your destination if needed and check "add to target" to your app.

这适用于我在Playground上没试过的项目。首先将声音拖到项目中,然后根据需要选择复制目的地并选中“添加到目标”到您的应用。

import Cocoa
import AVFoundation


var beepSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("beep", ofType: "aif")!)
func initBeep(){
    beepPlayer = AVAudioPlayer(contentsOfURL: beepSound, error: nil)
    beepPlayer.prepareToPlay()
}

func playBeepSound(){
    beepPlayer.play()
}
func applicationDidFinishLaunching(aNotification: NSNotification?) { 

    initBeep() 
}
@IBAction func btnPlayBeepSound(sender: AnyObject) {
   playBeepSound()
}