当前位置: 首页 > 面试题库 >

Xcode:下载mp3文件

呼延凌
2023-03-14
问题内容

我正在编写一个应用程序,可以在其中输入来自Web的mp3文件的URL,当我单击下载时,该应用程序会将其下载到应用程序中。有人能帮我吗
?我是ios开发的新手。我正在尝试学习Swift


问题答案:

Xcode 8•Swift 3

if let audioUrl = URL(string: "http://freetone.org/ring/stan/iPhone_5-Alarm.mp3") {

    // then lets create your document folder url
    let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

    // lets create your destination file url
    let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
    print(destinationUrl)

    // to check if it exists before downloading it
    if FileManager.default.fileExists(atPath: destinationUrl.path) {
        print("The file already exists at path")

        // if the file doesn't exist
    } else {

        // you can use NSURLSession.sharedSession to download the data asynchronously
        URLSession.shared.downloadTask(with: audioUrl) { location, response, error in
            guard let location = location, error == nil else { return }
            do {
                // after downloading your file you need to move it to your destination url
                try FileManager.default.moveItem(at: location, to: destinationUrl)
                print("File moved to documents folder")
            } catch {
                print(error)
            }
        }.resume()
    }
}


 类似资料:
  • 作为一名新的程序员,我一直在做大量的尝试和错误,并坚持使用那些有效的发现,但我目前使用的方法并不适用于所有浏览器。 我们的PHP代码。 HTML下载按钮。 你可以看到我使用的是Html5和上面的Javascript方法,这意味着它只支持FireFox和GoogleChrome。 如何允许所有浏览器上的用户点击我们的按钮下载文件?

  • 我正试图从Dropbox下载一些音频文件供用户下次在没有互联网的情况下使用,因此代码实际上下载了该文件,但我在播放该音频时遇到了问题,我不知道是否需要解析下载的文件或其他什么,希望您能提供帮助 下载文件并播放它类,它在执行后播放它,或者至少它尝试 我在日志上发现了这个错误 问题是试图播放下载的文件,即使尝试播放它与Android本地播放器它说"该播放器不支持这种类型的音频文件",所以我不知道我是否

  • 我们可以使用公共可下载的url来达到上述目的吗 谢谢

  • 以下是从mp3文件中提取内容和元数据的程序 - import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; impor

  • 下载 <?php /** * 下载文件请求示例 */ require dirname(__DIR__) . '/vendor/autoload.php'; use Yurun\Util\HttpRequest; use Yurun\Util\YurunHttp; use Yurun\Util\YurunHttp\Handler\Swoole; $url = 'http://www.baid