Loading media data asynchronously | Apple Developer Documentation
Build responsive apps by using language-level concurrency features to efficiently load media data.
언어 수준의 동시성 기능을 사용하여 미디어 데이터를 효율적으로 로드하여 반응형 앱을 빌드합니다.
AVFoundation uses the AVAsset
class to model timed audiovisual media. Creating an asset is a lightweight operation because it defers loading its media until it requires the data. How long it takes an asset to load its data depends on factors, including the media’s size, local device capabilities, and remote network conditions. To avoid blocking the calling thread, you must load media data asynchronously.
AVFoundation은 AVAsset
이 클래스를 사용하여 시간이 지정된 시청각 미디어를 모델링합니다. 자산을 만드는 것은 데이터가 필요할 때까지 미디어 로드를 연기하기 때문에 간단한 작업입니다. 자산이 데이터를 로드하는 데 걸리는 시간은 미디어의 크기, 로컬 장치 기능 및 원격 네트워크 상태를 포함한 요인에 따라 달라집니다. 호출 스레드가 차단되지 않도록 하려면 미디어 데이터를 비동기적으로 로드해야 합니다.
<aside> ⚠️
Important
Starting in iOS 16, tvOS 16, MacCatalyst 16, and macOS 13, AVFoundation deprecates using the synchronous properties and methods of AVAsset
, AVAssetTrack
, and AVMetadataItem
for Swift clients. It also deprecates loading property values asynchronously using the loadValuesAsynchronously(forKeys:completionHandler:)
method in favor of the syntax described below.
iOS 16, tvOS 16, MacCatalyst 16, macOS 13부터 AVFoundation은 Swift 클라이언트에서 AVAsset, AVAssetTrack, AVMetadataItem의 동기적 속성 및 메서드 사용을 더 이상 권장하지 않습니다. 또한, loadValuesAsynchronously(forKeys:completionHandler:) 메서드를 사용하여 속성 값을 비동기적으로 로드하는 방식도 더 이상 권장하지 않으며, 아래에 설명된 새로운 구문을 사용할 것을 권장합니다.
</aside>
The framework builds its asynchronous property-loading capabilities around two key types: AVAsyncProperty
and AVAsynchronousKeyValueLoading
. The framework uses the AVAsyncProperty
class to define type-safe identifiers for properties with values that require asynchronous loading, and uses the AVAsynchronousKeyValueLoading
protocol to define the interface for objects to load properties asynchronously. AVAsset
, AVAssetTrack
, and AVMetadataItem
adopt this protocol, which provides them an asynchronous load(_:)
method with the following signature:
AVFoundation은 비동기 속성 로딩 기능을 두 가지 주요 유형 AVAsyncProperty
인 및 AVAsynchronousKeyValueLoading
를 중심으로 구축합니다. AVAsyncProperty
클래스를 사용하여 비동기 로드가 필요한 값을 가진 속성에 대한 형식 안전 식별자를 정의하고, AVAsynchronousKeyValueLoading
프로토콜을 사용하여 개체가 속성을 비동기적으로 로드할 수 있는 인터페이스를 정의합니다. AVAsset
, AVAssetTrack
다음 서명이 있는 비동기 load(_:)
메서드를 제공하는 이 프로토콜을 AVMetadataItem
채택합니다.
public func load<T>(_ property: AVAsyncProperty<Self, T>) async throws -> T
Call this method from an asynchronous context, and specify the await
keyword to indicate that execution can suspend until it finishes loading the data. The method returns a type-safe value if it successfully loads the property, or throws an error if it fails.
이 메서드는 비동기 컨텍스트에서 호출하고, await 키워드를 지정하여 데이터 로드가 완료될 때까지 실행이 일시 중지될 수 있음을 나타냅니다. 메서드는 속성을 성공적으로 로드하면 type-safe한 값을 반환하고, 실패하면 오류를 던집니다.
// A CMTime value.
let duration = try await asset.load(.duration)
// An array of AVMetadataItem for the asset.
let metadata = try await asset.load(.metadata)
If you know in advance that you require loading several asset properties, you can use a variation of the load(_:)
method that takes multiple identifiers and returns its result in a tuple. Like loading a single property value, loading several properties at the same time is also a type-safe operation.