Swift AVAudioPlayer不会播放用AVAudioRecorder录制的音频 [英] Swift AVAudioPlayer wont play audio recorded with AVAudioRecorder

查看:138
本文介绍了Swift AVAudioPlayer不会播放用AVAudioRecorder录制的音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个记录音频剪辑的应用程序,并将其保存到名为 xxx.m4a 的文件中。

I have built an app that records an audio clip, and saves it to a file called xxx.m4a.

录制完成后,我可以在我的系统上找到这个 xxx.m4a 文件并播放 - 然后播放回来。问题是没有录制这个音轨。

Once the recording is made, I can find this xxx.m4a file on my system and play it back - and it plays back fine. The problem isn't recording this audio track.

我的问题是在应用程序内播放此音轨。

My problem is playing this audio track back from within the app.

// to demonstrate how I am building the recordedFileURL
let currentFileName = "xxx.m4a"
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsDir: AnyObject = dirPaths[0]
let recordedFilePath = docsDir.stringByAppendingPathComponent(currentFileName)
var recordedFileURL = NSURL(fileURLWithPath: recordedFilePath)

// quick check for my own sanity
var checkIfExists = NSFileManager.defaultManager()
if checkIfExists.fileExistsAtPath(recordedFilePath){
    println("audio file exists!")
}else{
    println("audio file does not exist")
}

// play the recorded audio
var error: NSError?
let player = AVAudioPlayer(contentOfURL: recordedFileURL!, error: &error)
if player == nil {
    println("error creating avaudioplayer")
    if let e = error {
        println(e.localizedDescription)
    }
}
println("about to play")
player.delegate = self
player.prepareToPlay()
player.volume = 1.0
player.play()
println("told to play")

// -------

// further on in this class I have the following delegate methods:
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool){
    println("finished playing (successfully? \(flag))")
}
func audioPlayerDecodeErrorDidOccur(player: AVAudioPlayer!, error: NSError!){
    println("audioPlayerDecodeErrorDidOccur: \(error.localizedDescription)")
}

运行此代码时我的控制台输出如下:

My console output when I run this code is like this:

audio file exists!
about to play
told to play

我没有登录到控制台来自 audioPlayerDidFinishPlaying audioPlayerDecodeErrorDidOccur

I dont get anything logged into the console from either audioPlayerDidFinishPlaying or audioPlayerDecodeErrorDidOccur.

有人可以向我解释为什么我的音频片段没有播放?

Can someone explain to me why I my audio clip isnt playing?

非常感谢。

推荐答案

你玩代码很好;你唯一的问题是你的 AVAudioPlayer 对象的范围。基本上,你需要在玩家的所有时间都保持强烈的参考,否则它会被ARC摧毁,因为它会认为你不再需要它了。

You playing code is fine; your only problem is with the scope of your AVAudioPlayer object. Basically, you need to keep a strong reference to the player around all the time it's playing, otherwise it'll be destroyed by ARC, as it'll think you don't need it any more.

通常你会使AVAudioPlayer对象成为你的代码所在类的属性,而不是使它成为方法中的局部变量。

Normally you'd make the AVAudioPlayer object a property of whatever class your code is in, rather than making it a local variable in a method.

这篇关于Swift AVAudioPlayer不会播放用AVAudioRecorder录制的音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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