Я создал приложение, которое записывает аудиоклип и сохраняет его в файл с именем xxx.m4a
.Swift AVAudioPlayer не воспроизводит звук, записанный с помощью AVAudioRecorder
Как только запись будет сделана, я могу найти этот файл xxx.m4a
в моей системе и воспроизвести его обратно - и он отлично воспроизводится. Проблема заключается не в записи этой звуковой дорожки.
Моя проблема заключается в воспроизведении этой звуковой дорожки из приложения.
// 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)")
}
Мой вывод на консоль при запуске этого кода, как это:
audio file exists!
about to play
told to play
я не получаю ничего зарегистрированному в консоли из любой audioPlayerDidFinishPlaying
или audioPlayerDecodeErrorDidOccur
.
Может кто-нибудь объяснить мне, почему я не слышу мой аудиоклип?
Большое спасибо.
В какой области находится ваш код игрока? Имейте в виду, что если ваш экземпляр игрока является переменной в функции, например, он будет недоступен, как только функция выйдет, и, следовательно, кандидат для ARC, чтобы собрать свою память, которая, вероятно, уничтожит ее до этого начинает играть ... (Попробуйте сделать свой объект 'player' собственностью класса, это то, что я говорю, поэтому он висит на протяжении всего времени ...) –
@MattGibson это работало как шарм - огромное спасибо! Отправьте это как правильный ответ, и я сделаю вас верным. – Jimmery
Полезно знать. Готово. –