Expand description
§avplayer
Safe Rust bindings for Apple’s AVFoundation playback stack on macOS: AVPlayer, AVPlayerItem, AVPlayerLayer, AVQueuePlayer, AVPlayerLooper, AVAsset, AVURLAsset, and AVAssetReader.
Status:
0.3.0adds the Tier-1async_apimodule (feature-gated behindasync) wrapping AVFoundation’sasync throwsand completion-handler APIs as executor-agnostic RustFutures.0.2.2brought the crate to 100 % coverage of the audited macOS playback surface. SeeCOVERAGE.md.
§Quick start
use avplayer::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let asset = UrlAsset::from_file_path("target/example-artifacts/test.aiff")?;
asset.load_values_asynchronously(["duration", "tracks", "metadata"])?;
let player = Player::from_asset(asset.as_asset())?;
player.set_action_at_item_end(PlayerActionAtItemEnd::Pause)?;
println!("duration: {:?}", asset.duration()?);
println!("tracks: {}", asset.tracks()?.len());
println!("time control: {:?}", player.time_control_status()?);
player.play();
player.pause();
Ok(())
}§Async API
Enable the async Cargo feature for executor-agnostic Future wrappers around
AVFoundation’s async throws and completion-handler APIs:
avplayer = { version = "0.3", features = ["async"] }use avplayer::{UrlAsset, async_api::AsyncAsset};
fn main() -> Result<(), Box<dyn std::error::Error>> {
pollster::block_on(async {
let asset = UrlAsset::from_file_path("my.mp4")?;
let props = AsyncAsset::new(asset.as_asset()).load_properties().await?;
println!("playable={} duration={:?}", props.is_playable, props.duration);
let tracks = AsyncAsset::new(asset.as_asset()).load_tracks().await?;
println!("{} tracks", tracks.len());
Ok(())
})
}| Type | API wrapped |
|---|---|
async_api::AsyncAsset | AVAsset.load(...), loadTracks(withMediaType:), loadTrack(withTrackID:) |
async_api::AsyncPlayerItem | AVPlayerItem.seek(to:completionHandler:) |
async_api::AsyncPlayer | AVPlayer.seek(to:completionHandler:), preroll(atRate:completionHandler:) |
KVO / notification streams (multi-fire) belong to a Tier-2 pattern and are not included in this module.
§Highlights
AVAsset/AVURLAsset: async key loading, URL inspection, metadata, track enumeration, andUrlAssetOptions.AVPlayer: play/pause/rate/seek, volume + mute, action-at-item-end, time-control/waiting-state inspection, time observers, rate-change observation, HDR/background/network policy access, and media-selection criteria application.AVPlayerItem: observation callbacks (including time-jumped / failed-to-end / live-offset changes), buffering/bit-rate/resolution preferences, variant preferences, protected-content authorization status, custom compositor info, outputs, and per-item logs.AVPlayerLayer: player attachment, video gravity, video rect inspection, and displayed pixel-buffer access.AVQueuePlayer/AVPlayerLooper: queue mutation, current-item inspection, loop configuration, and loop-state reporting.AVPlayerItemOutput,AVPlayerItemVideoOutput,AVPlayerItemMetadataOutput, andAVPlayerItemLegibleOutput: attach/detach, base-output timing/suppression helpers, delegate observation, and text-styling/output configuration helpers.AVPlayerItemTrack,AVPlayerItemAccessLog,AVPlayerItemErrorLog, andAVPlayerMediaSelectionCriteriawrappers.AVAssetReader,AssetReaderTrackOutput,AssetReaderAudioMixOutput, andAssetReaderVideoCompositionOutputremain available for frame/sample extraction.
§Examples
Every requested subsystem area now has a numbered example:
01_smoke_surface02_avasset03_avurlasset04_avplayer05_avplayer_item06_avplayer_layer07_avqueue_player08_avplayer_looper09_avplayer_item_access_log10_avplayer_item_error_log11_avplayer_item_metadata_output12_avplayer_item_video_output13_avplayer_item_legible_output14_avplayer_item_track15_avplayer_media_selection_criteria
Run any example with:
cargo run --example 15_avplayer_media_selection_criteriaExamples write synthesized media into target/example-artifacts/ and avoid /tmp.
§Notes
AVPlayerItemTrackmaterialization is media- and readiness-dependent. On synthesizedAIFFs,AVFoundationmay legitimately report zeroAVPlayerItemTrackinstances until it fully prepares the item.- The current macOS SDK used for this release does not expose
AVPlayerItem.externalMetadata, soPlayerItem::metadata()continues to surface the underlying asset metadata.
§License
Licensed under either of Apache-2.0 or MIT at your option.
Modules§
- async_
api async - Async API for
avplayer— executor-agnosticFuturewrappers forAVFoundation’sasync throwsand completion-handler surfaces. - ffi
- Raw FFI declarations matching
swift-bridge/Sources/AVPlayerBridge. - prelude
- Common imports.
Structs§
- Affine
Transform - Asset
- Safe wrapper around
AVAsset. - Asset
Reader - Safe wrapper around
AVAssetReader. - Asset
Reader Audio MixOutput AVAssetReaderAudioMixOutput.- Asset
Reader Track Output AVAssetReaderTrackOutput.- Asset
Reader Video Composition Output AVAssetReaderVideoCompositionOutput.- Asset
Track - Safe wrapper around
AVAssetTrack. - Audio
Output Settings - Builder for linear-PCM audio
AVAssetReaderoutput settings. - Boundary
Time Observer - RAII token for
addBoundaryTimeObserver. - Date
Range Metadata Group - KeyLoad
Status - Result for a single key passed to
load_values_asynchronously. - Metadata
Collector Observer - Metadata
Item - Simplified
AVMetadataItemview suitable for asset/player inspection. - Metadata
Output Observer - Periodic
Time Observer - RAII token for
addPeriodicTimeObserver. - Player
- Safe wrapper around
AVPlayer. - Player
Integrated Timeline OutOf Sync Event - Player
Interstitial Event - Player
Interstitial Event Controller - Player
Interstitial Event Info - Player
Interstitial Event Monitor - Player
Interstitial Event Monitor Observer - Player
Interstitial Event Monitor State - Player
Interstitial Event Restrictions - Player
Item - Safe wrapper around
AVPlayerItem. - Player
Item Access Log - Player
Item Access LogEvent - Player
Item Error Log - Player
Item Error LogEvent - Player
Item Integrated Timeline - Player
Item Integrated Timeline Info - Player
Item Integrated Timeline Observer - Player
Item Integrated Timeline Segment - Player
Item Integrated Timeline Segment Info - Player
Item Integrated Timeline Snapshot - Player
Item Integrated Timeline Snapshot Info - Player
Item Legible Output - Player
Item Legible Output Observer - Player
Item Media Data Collector Info - Player
Item Metadata Collector - Player
Item Metadata Output - Player
Item Observer - KVO + notification observer for
AVPlayerItem. - Player
Item Output - Borrowed view of the abstract
AVPlayerItemOutputbase class. - Player
Item Rendered Legible Output - Player
Item Track - Player
Item Video Compositor Info - Player
Item Video Output - Player
Item Video Output Observer - Player
Layer - Player
Looper - Player
Media Selection Criteria - Player
Rate DidChange Event - Player
Rate DidChange Observer - Player
Video Output - Player
Video Output Configuration - Player
Video Output Sample - Player
Video Output TagCollection - Player
Video Tagged Buffer - Queue
Player - Rect
- Rendered
Caption Image - Rendered
Legible Output Observer - Size
- Serializable
CGSizemirror. - Time
Range - Serializable representation of
CMTimeRange. - Timed
Metadata Group - UrlAsset
AVURLAssetconvenience wrapper aroundAsset.- UrlAsset
Options - Variant
Preferences - Video
Output Settings - Builder for uncompressed video
AVAssetReaderoutput settings. - Video
Output Specification
Enums§
- AVPlayer
Error - Top-level error type returned by fallible APIs in this crate.
- Asset
Reader Status AVAssetReaderStatus.- Audio
Time Pitch Algorithm - Content
Authorization Status - KeyValue
Status - Per-key loading state returned by
AVAsynchronousKeyValueLoading. - Media
Characteristic - Media
Type - Simplified media-type classification for asset tracks and reader outputs.
- Metadata
Collector Event - Metadata
Output Event - Player
Action AtItem End - Player
Audiovisual Background Playback Policy - Player
Integrated Timeline Snapshots OutOf Sync Reason - Player
Interstitial Event Asset List Response Status - Player
Interstitial Event Cue - Player
Interstitial Event Monitor Event - Player
Interstitial Event Skippable Event State - Player
Interstitial Event Timeline Occupancy - Player
Item Event - Events emitted by
PlayerItemObserver. - Player
Item Legible Output Event - Player
Item Legible Output Text Styling Resolution - Player
Item Media Data Collector Kind - Player
Item Segment Type - Player
Item Status AVPlayerItemStatus.- Player
Item Track Video Field Mode - Player
Item Video Output Event - Player
Looper Item Ordering - Player
Looper Status - Player
Network Resource Priority - Player
Rate DidChange Reason - Player
Status AVPlayerStatus.- Player
Time Control Status - Player
Video Output TagCollection Preset - Player
Video Tagged Buffer Kind - Player
Waiting Reason - Rendered
Legible Output Event - Time
- Serializable representation of
CMTime. - Video
Gravity
Functions§
- player_
eligible_ for_ hdr_ playback_ did_ change_ notification - player_
integrated_ timeline_ snapshots_ out_ of_ sync_ notification - player_
integrated_ timeline_ snapshots_ out_ of_ sync_ reason_ current_ segment_ changed - player_
integrated_ timeline_ snapshots_ out_ of_ sync_ reason_ key - player_
integrated_ timeline_ snapshots_ out_ of_ sync_ reason_ loaded_ time_ ranges_ changed - player_
integrated_ timeline_ snapshots_ out_ of_ sync_ reason_ segments_ changed - player_
waiting_ during_ interstitial_ event_ reason