Skip to content

A highly customizable player that uses AVPlayer and VLCPlayer for playback

License

Notifications You must be signed in to change notification settings

xueqooy/XPlayback

Repository files navigation

截屏2024-12-20 14 59 26

XPlayback is a hybrid media player designed to play audio, video, and embedded videos (e.g., YouTube Embed).

Features

  • ▶️ Audio and Video Playback
  • 📹 Embedded Video playback (youtube embed)
  • ⚙️ Customizable Engine
  • 🕹️ Customizable Control Layer
  • 🔌 Plugin Architecture
  • 📱 Screen Rotation Adaptation
  • 👆🏻 Gesture Controls
  • 🖥️ Fullscreen Playback
  • 🖼️ Seeking Video Preview
  • 🎶 Multi-quality Asset
  • ⚒️ Player reuse mechanism

Usage

1. Attach the player:

  • The player will share between the same item.
  • The player view size matches the container view.
  • Only one item can play at a time; playing a new item pauses others.
  • Previous players are removed from the container when a new player is attached.
// Identical playing content is distinguished by `tag`, for example, different cells in the list play the same content.
let playbackItem = PlaybackItem(type: .video, contentString: videoURLString, tag: tag)
playbackService.attachPlayer(to: videoContainerView, with: playbackItem)

// Manually remove the player
playbackService.removePlayer(from: videoContainerView)

// Pause or stop all player
playbackService.pauseAllPlayers()
playbackService.stopAllPlayers()

// Play, pause or stop the specific player
playbackService.playPlayer(for: item)
playbackService.pausePlayer(for: item)
playbackService.stopPlayer(for: item)

2. Pause playback when the player moves off-screen in a scroll view:

// for video
playbackService.preferences.shouldAutoPauseVideoOnScrollView = true
// for audio
playbackService.preferences.shouldAutoPauseAudioOnScrollView = true

3. Adapt to device rotation:

If the app only supports portrait mode but you want to autorotate fullscreen when the device orientation changes:

playbackService.preferences.orientationsForApplyingRotateTransform = [.landscapeLeft, .landscapeRight, .portraitUpsideDown]

If the app supports all orientations, set:

playbackService.preferences.orientationsForApplyingRotateTransform = []

4. Customize video presentation view:

To customize the video player view, implement a new class that conforms to the VideoPresentable protocol and set it to

playbackService.preferences.videoPresentationViewType

5 Customize playback control view:

To customize the playback control view, implement a new class that conforms to the PlaybackControllable protocol and set it to

// for video
playbackService.preferences.videoControlType
// for audio
playbackService.preferences.audioControlType

6. Customize playback control plugins:

To customize the playback control plugins, create a new type that conforms to the PlayerPluginSet protocol and set it to

// for video
playbackService.preferences.videoPlayerPluginSet
// for audio
playbackService.preferences.audioPlayerPluginSet 

7 Customize item parser:

To customize the item parser, implement a new class that conforms to the PlaybackItemParseable protocol and add it to

PlaybackURLManager.shared.additionalParsers

8. Directly use the player:

If you want to manage the player yourself and do not want the player to be shared or removed when not needed, you can directly use the HybridMediaPlayer or EmbedVideoPlayer, which provide direct playback functionality.

// Play video or audio
let hint = PlaybackHint(format: media.format, title: resource.title)
let engineType: PlayerEngineType = media.isAVPlayerSupportedFormat ? .av : .vlc
let player: HybridMediaPlayer = media.isAudio ? .defaultAudioPlayer(engineType: engineType) : .defaultVideoPlayer(engineType: engineType)
player.hint = hint
player.url = media.url
player.containerView = contaienrView

// Multi-quality asset
let multiQualityAsset = MultiQualityAsset(items: [
  .init(url: url1, label: "720p"),
  .init(url: url2, label: "1080p"),
  .init(url: url3, label: "4K")
], defaultIndex: 1)
player.multiQualityAssetController?.asset = multiQualityAsset

// Play youtube embed video
let player = EmbedVideoPlayer()
player.url = youtubeEmbedURL
player.containerView = contaienrView

Run Examples

The local media file for Example can be configured using the Resource.swift file. To add a local file, place it in the sample directory and name it according to the enumeration value. For remote resources, simply define the enumeration value with the rawValue being the resource URL.

Snapshot

截屏2024-12-17 15 23 17

截屏2024-12-17 15 26 03

截屏2024-12-17 15 23 39

TODO

  • Now playing
  • Remote command control
  • SPM support

License

XPlayback is licensed under the MIT License. See LICENSE for more information.

Contact

About

A highly customizable player that uses AVPlayer and VLCPlayer for playback

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published