从文档目录播放m4a文件 [英] Play m4a file from document Directory

查看:94
本文介绍了从文档目录播放m4a文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想播放一个m4a文件,但在控制台中出现错误,该文件将无法播放.

I would to play an m4a file but I'm getting an error in the console and the file won't play.

控制台打印:

操作无法完成. (OSStatus错误2003334207.)

The operation couldn’t be completed. (OSStatus error 2003334207.)

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
print("File Location: ",url.path)

if(FileManager.default.fileExists(atPath: url.path)) {
   do {
       audioPlayer = try AVAudioPlayer(contentsOf: url)
       guard let player = audioPlayer else { return }

       player.prepareToPlay()
   } catch let error {
       print(error.localizedDescription)
   }

} else {
    print("file not found")
}

我尝试过这样的网址:

let NoteUrl = Bundle.main.url(forResource: "Voice Note", withExtension: "m4a")

但是我无法播放.有人可以帮我按文件名播放m4a文件吗?任何帮助将不胜感激.

But I couldn't get it to play. Can someone help me to play m4a file by file's name? Any help will be appreciated.

推荐答案

您正在将Documents文件夹传递到音频播放器.您的代码需要将特定的文件名附加到Documents文件夹中.

You are passing the Documents folder to the audio player. Your code needs to append the specific filename to the Documents folder.

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let url = documentsURL.appendingPathComponent("Voice Note.m4a")

这篇关于从文档目录播放m4a文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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