[英]The Operation Couldn’t Be Completed. (OSStatus Error 2003334207.)
I know this is a duplicate question but I did not find any solution that works for me.我知道这是一个重复的问题,但我没有找到任何适合我的解决方案。
I am using AVAudioPlayer to play an audio with the url which I am getting from Firebase .我正在使用AVAudioPlayer使用url播放音频,我从Firebase获得。 The url I get from Firebase is:
我从Firebase得到的url是:
https://firebasestorage.googleapis.com:443/v0/b/unified-45b5c.appspot.com/o/AudioMessage%2F5%2B8_5_Audio_07-09-20224:07:30.m4a?alt=media&token=af3aabce-46bd-4d55-a7ca-30623eaf1817
https://firebasestorage.googleapis.com:443/v0/b/unified-45b5c.appspot.com/o/AudioMessage%2F5%2B8_5_Audio_07-09-20224:07:30.m4a?alt=media&token=af3aabce-46bd- 4d55-a7ca-30623eaf1817
if I open this url on google chrome it gives me my audio.如果我在谷歌浏览器上打开这个 url它会给我我的音频。 But when I try it using AVAudioPlayer it gives me the following error:
但是当我使用AVAudioPlayer尝试它时,它给了我以下错误:
The operation couldn't be completed.
操作无法完成。 (OSStatus error 2003334207.)
(OSStatus 错误 2003334207。)
This is what I am doing:这就是我正在做的事情:
Button {
vm.startPlaying(url: audioURL ?? URL(fileURLWithPath: ""))
} label: {
Image(systemName: startAudio ? "pause.fill" : "play.fill")
.foregroundColor(.black)
.padding()
.background(
RoundedRectangle(cornerRadius: 10)
.foregroundColor(.gray)
.frame(width: 100)
)
}
And the function startPlaying() :和 function startPlaying() :
func startPlaying(url : URL) {
do {
audioPlayer = try AVAudioPlayer(contentsOf : url)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
Helper.shared.printDebug("Playing Failed")
Helper.shared.printDebug(error.localizedDescription) // error: The operation couldn’t be completed. (OSStatus error 2003334207.)
}
}
The initializer you are using for AVAudioPlayer expect an URL to a local file.您用于 AVAudioPlayer 的初始化程序需要 URL 到本地文件。 You can't pass it a URL to a file on a remote server.
您不能将 URL 传递给远程服务器上的文件。 To quote the docs:
引用文档:
Declaration
init(contentsOf url: URL) throws
Parameters
url
A URL that identifies the local audio file to play
You'll need to download the data to a local file before playing it, or use a method that supports streaming audio playback.您需要在播放之前将数据下载到本地文件,或使用支持流式音频播放的方法。
Duncan's solution works if you want to download the item first, and then play it.如果您想先下载该项目,然后再播放,则 Duncan 的解决方案有效。 If you want more like a streaming experience, you can useAVPlayer , which is built to allow playing remote files, and helps with streaming:
如果您想要更像流媒体体验,您可以使用AVPlayer ,它旨在允许播放远程文件,并有助于流媒体:
Use an instance of AVPlayer to play local and remote file-based media, such as QuickTime movies and MP3 audio files, as well as audiovisual media served using HTTP Live Streaming.
使用 AVPlayer 的实例播放本地和远程基于文件的媒体,例如 QuickTime 电影和 MP3 音频文件,以及使用 HTTP 实时流媒体提供的视听媒体。
let player = AVPlayer(playerItem: item)
let audio = AVPlayerItem(url: url)
player.play()
Note that you can keep an instance of the player to play multiple items请注意,您可以保留播放器的一个实例来播放多个项目
See this answer for detailed comparison between the two有关两者之间的详细比较,请参阅此答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.