LoginSignup
0
0

【Swift】動画DLの進捗をプログレスバーで表示する

Posted at

概要

ネットから動画をDLして進捗をプログレスバーで見れるようにするサンプルです。

プログレスバー追加

まず初めに進捗を表示するプログレスバーを画面に追加します。

ViewController.swift
import UIKit

class ViewController: UIViewController {
    var progressView: UIProgressView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        progressView = UIProgressView(progressViewStyle: .default)
        progressView.center = view.center
        progressView.progress = .zero
        view.addSubview(progressView)
    }
}

DL関数

次にDLを開始する関数を実装します。
DLには以下のURLを使いました。

ViewController.swift
private func startDownload() {
    let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
    let downloadTask = session.downloadTask(with: videoURL)
    downloadTask.resume()
}

DL完了検知

次にDL完了時に画面を更新します。
urlSession(_:downloadTask:didFinishDownloadingTo:)を使って検知できます。

DLが完了したらプログレスバーの進捗を100%にします。

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    DispatchQueue.main.async {
        self.progressView.progress = 1.0
    }
}

DL進捗検知

DLの進捗をリアルタイムに取得して画面に反映していきます。

urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)を使ってDLの進捗を検知できます。

func urlSession(
    _ session: URLSession,
    downloadTask: URLSessionDownloadTask,
    didWriteData bytesWritten: Int64,
    totalBytesWritten: Int64,
    totalBytesExpectedToWrite: Int64
) {
    let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)

    DispatchQueue.main.async {
        self.progressView.progress = progress
    }
}

全体のコードはこちらです。

ViewController.swift
import UIKit

class ViewController: UIViewController, URLSessionDownloadDelegate {
    var progressView: UIProgressView!
    
    private let videoURL = URL(string: "https://www.home-movie.biz/mov/hts-samp001.mp4")!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        progressView = UIProgressView(progressViewStyle: .default)
        progressView.center = view.center
        progressView.progress = .zero
        view.addSubview(progressView)
        
        startDownload()
    }
    
    private func startDownload() {
        let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
        let downloadTask = session.downloadTask(with: videoURL)
        downloadTask.resume()
    }
    
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        DispatchQueue.main.async {
            self.progressView.progress = 1.0
        }
    }
    
    func urlSession(
        _ session: URLSession,
        downloadTask: URLSessionDownloadTask,
        didWriteData bytesWritten: Int64,
        totalBytesWritten: Int64,
        totalBytesExpectedToWrite: Int64
    ) {
        let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
        
        DispatchQueue.main.async {
            self.progressView.progress = progress
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0