ios – 只能从mp3文件下载几秒钟

ios – 只能从mp3文件下载几秒钟,第1张

概述我需要下载一个mp3文件,但是我只需要前20秒的歌曲(或者整首歌曲,如果歌曲不到20秒). 这是我如何下载整首歌曲: func downloadSong(audioUrl: URL) { let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).fi 我需要下载一个mp3文件,但是我只需要前20秒的歌曲(或者整首歌曲,如果歌曲不到20秒).
这是我如何下载整首歌曲:
func downloadSong(audioUrl: URL) {    let documentsDirectoryURL = fileManager.default.urls(for: .documentDirectory,in: .userDomainMask).first!    let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)    URLSession.shared.downloadTask(with: audioUrl,completionHandler: { (location,response,error) -> VoID in        guard let location = location,error == nil else { return }        do {            try fileManager.default.moveItem(at: location,to: destinationUrl)            // song available in destinationUrl        } catch let error as NSError {        }    }).resume()}

有没有办法在20秒后停止下载?
我知道我可以下载整首歌,然后剪下来,但是我想要更有效率,特别是如果这首歌很长.

解决方法

MP3 file is divIDed into a small blocks – frames. Each frame has
constant time length 0.026 sec. But size of one frame (in Bytes)
varIEs according to bitrate. Eg. for 128kbps it is (normally) 417
Bytes and for 192kbps 626 Bytes. The first 4 Bytes of each frame is
frame header and the rest is audio data.

起价为here.

并且每个框架都可以播放.因此,对于20秒的数据,您需要大约770帧(20 / 0.026).
所以先下载770帧,然后取消下载任务.然后播放您下载的帧.

或按尺寸,先下载128kbps文件的313.5 KB(计算:(417字节* 770)/ 1024)数据.你会得到你20秒的数据.

总结

以上是内存溢出为你收集整理的ios – 只能从mp3文件下载几秒钟全部内容,希望文章能够帮你解决ios – 只能从mp3文件下载几秒钟所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1113405.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存