Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AVFoundation-播放wav文件(应用程序包外)Swift 2.1_Swift_Macos_Swift2_Command Line Interface_Avaudioplayer - Fatal编程技术网

AVFoundation-播放wav文件(应用程序包外)Swift 2.1

AVFoundation-播放wav文件(应用程序包外)Swift 2.1,swift,macos,swift2,command-line-interface,avaudioplayer,Swift,Macos,Swift2,Command Line Interface,Avaudioplayer,我只是(第一次)尝试一下Swift。除了错误检查和正确的应用程序结构之外,我认为应该播放音频: import Foundation import AVFoundation var audioPlayer: AVAudioPlayer! let file = "/Users/mtwomey/Desktop/test1/test1/a2002011001-e02.wav" let url = NSURL(fileURLWithPath: file) print(url) audioPlayer

我只是(第一次)尝试一下Swift。除了错误检查和正确的应用程序结构之外,我认为应该播放音频:

import Foundation
import AVFoundation

var audioPlayer: AVAudioPlayer!
let file = "/Users/mtwomey/Desktop/test1/test1/a2002011001-e02.wav"

let url = NSURL(fileURLWithPath: file)
print(url)
audioPlayer = try! AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: file), fileTypeHint: "wav")
audioPlayer.play()
print("Done.")
但事实并非如此。当我运行这个应用程序时,它只是继续打印“完成”,然后退出。如果文件名/文件路径不正确,我会得到一个异常(因此它看起来实际上正在访问该文件)

我试图证明一个控制台应用程序的概念,它需要访问应用程序包之外的wave文件。有关于我遗漏什么的提示吗?

试试这个

import UIKit
import AVFoundation

class ViewController: AVAudioPlayerDelegate{
var audioPlayer: AVAudioPlayer!  // Declaring this outside your function, as class variable is important! otherwise your player won't be able to play the sound.

override func viewDidLoad() {
    super.viewDidLoad()
    self.playSound("/Users/mtwomey/Desktop/test1/test1/a2002011001-e02.wav")
}

func playSound(soundPath: String)
{
    let sound = NSURL(fileURLWithPath: soundPath)
    do{
        audioPlayer = try AVAudioPlayer(contentsOfURL: sound, fileTypeHint: "wav")
        audioPlayer.prepareToPlay()
        audioPlayer.delegate = self
        audioPlayer.play()
    }catch {
        print("Error getting the audio file")
    }
}
或者(如果您已经在项目中放置了文件)(按照此操作了解如何在项目中放置文件)


我发现在执行swift文件时无法播放音频的主要问题是:命令行工具在AVAudioPlayer准备播放之前(异步操作完成之前)退出

这是我的解决方案(使用DispatchGroup),它可以通过从命令行工具执行我的Swift文件(用Swift 5.x编写)来播放我的mp3文件


操场都是沙箱。您是否在实际的OSX项目中试用过它?@LeoDabus-是的,我正在Xcode(7.1.1)中运行它,上面是单个源文件(main.swift)的全部内容。我指的是带有窗口/视图的完整项目,而不是playground@LeoDabus-这将用于命令行实用程序,不会有任何窗口/视图。也许我误解了?(很可能)只是为了进一步澄清-这是一个项目,而不是一个游乐场。这是一个Mac控制台/命令行应用程序,但(不是手机)-我正在尝试调整从您的答案中得到的提示。我不知道,也许这只是一个错误的用例。也许我应该回到控制台应用程序的C++。因为这是关于音频的,我想我可以用Swift节省时间。
import UIKit
import AVFoundation

class ViewController: AVAudioPlayerDelegate{
var audioPlayer: AVAudioPlayer!  // Declaring this outside your function, as class variable is important! otherwise your player won't be able to play the sound.

override func viewDidLoad() {
    super.viewDidLoad()
    self.playSound("sound-name")
}

func playSound(soundName: String)
{
    let sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundName, ofType: "wav")!)
    do{
        audioPlayer = try AVAudioPlayer(contentsOfURL: sound, fileTypeHint: "wav")
        audioPlayer.prepareToPlay()
        audioPlayer.delegate = self
        audioPlayer.play()
    }catch {
        print("Error getting the audio file")
    }
}
import Foundation
import AVFoundation

var testAudio = AVAudioPlayer()

let path = FileManager.default.currentDirectoryPath + "/example.mp3"

let url = URL(fileURLWithPath: path)

let group = DispatchGroup()
group.enter()


DispatchQueue.main.async {
    do {
        testAudio = try AVAudioPlayer(contentsOf: url)
    } catch let err {
        print("Failed play audio: \(err)")
    }
    
    group.leave()
}

group.notify(queue: .main) {
    print("play audio")
    testAudio.play()
}

dispatchMain()