SuperPlayer is a library to wrap AVPlayer with Composable Architecture.

Overview

SuperPlayer

SuperPlayer is a library to wrap AVPlayer with Composable Architecture. It can be used in SwiftUI and UIKit.

Learn More

AVPlayer a controller object used to manage the playback and timing of a media asset. You can use an AVPlayer to play local and remote file-based media, such as QuickTime movies and MP3 audio files, as well as audiovisual media served using HTTP Live Streaming.

This SuperPlayer is composing AVPlayer to a TCA component. All state of the player is handled by SuperPlayer, and you just need to listen it to your controller, view, or other wrapper.

What is the Composable Architecture?

The Composable Architecture (TCA, for short), is a library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind. It can be used in SwiftUI, UIKit, and more, and on any Apple platform (iOS, macOS, tvOS, and watchOS). You can learn more about composable architecture from this repository

Example

@alvin.pratama made a tutorial of how to try this Superplayer on this medium

Basic Usage

Let’s say we will play the video from this link http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4

Open Your ViewController, and import ComposableArchitecture and SuperPlayer

import ComposableArchitecture
import SuperPlayer

Create a function called handleVideo() , we will handle video setup in this function.

func handleVideo() {
    // 1
    let store = Store<SuperPlayerState, SuperPlayerAction>(initialState: SuperPlayerState(), reducer: superPlayerReducer, environment: SuperPlayerEnvironment.live())
    let superPlayer = SuperPlayerViewController(store: store)

    // 2
    superPlayer.view.frame = view.bounds
    superPlayer.view.backgroundColor = .black
    addChild(superPlayer)
    view.addSubview(superPlayer.view)
    superPlayer.didMove(toParent: self)

    // 3
    let viewStore = ViewStore(store)
    viewStore.send(.load(URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")!, autoPlay: true))
}
  1. SuperPlayerViewController will act as the intermediate object that performs the actual subscription to both store and the AVFoundation playback states. SuperPlayerViewController has AVPlayerLayer as its sublayer to display the video from AVPlayer.
  2. Constructing view for SuperPlayerViewController
  3. Send the action to load the video.

Call this function inside viewDidLoad before any view setup made.

SuperPlayer is supposed to working with TCA, so why not create our TCA state management to play the video. Create Reducer.swift file for your controller

= .combine( superPlayerReducer .pullback( state: \.superPlayerState, action: /AppAction.superPlayerAction, environment: { _ in SuperPlayerEnvironment.live() } ), Reducer { state, action, env in switch action { case .loadVideo: return Effect(value: .superPlayerAction(.load(URL(string: env.getVideoURLString())!, autoPlay: true))) default: return .none } } ) ">
import Foundation
import ComposableArchitecture
import SuperPlayer // 1
// 2
struct AppState: Equatable {
    var superPlayerState: SuperPlayerState = .init()
}

enum AppAction: Equatable {
    case loadVideo
    case handlePausePlayVideo
    
    case superPlayerAction(SuperPlayerAction)
}

// 3
struct AppEnvironment {
    var getVideoURLString: () -> String
    
    static var mock = AppEnvironment(
        getVideoURLString: {
            "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
        }
    )
}

// 4
let appReducer: Reducer 
   = .
   combine(
    superPlayerReducer
        .
   pullback(
            
   state: \.
   superPlayerState,
            
   action: 
   /AppAction.
   superPlayerAction,
            
   environment: { 
   _ 
   in SuperPlayerEnvironment.
   live() }
        ),
    Reducer { state, action, env 
   in
        
   switch action {
        
   case .
   loadVideo
   :
            
   return 
   Effect(
   value: .
   superPlayerAction(.
   load(
   URL(
   string: env.
   getVideoURLString())
   !, 
   autoPlay: 
   true)))
            
        
   default
   :
            
   return .
   none
        }
    }
)
  
  1. Import SuperPlayer
  2. Add SuperPlayerState and SuperPlayerAction to pullback SuperPlayerReducer. loadVideo action will act as a trigger to load the video when we call it from ViewController later.
  3. Usually, the environment is used for producing Effects such as API clients, analytics clients, etc. For this case let’s directly return the video URL we want to play.
  4. Combine appReducer and superPlayerReducer to merge all the effects. We create a pullback to transforms superPlayerReducer into state management that works on global state management.

Go back to your view controller file and set up the store.

// 1
let store = Store(
    initialState: AppState(),
    reducer: appReducer,
    environment: AppEnvironment.mock
)
lazy var viewStore = ViewStore(self.store)

override func viewDidLoad() {
    super.viewDidLoad()
    
    // 2
    handleVideo(store: store.scope(
        state: \.superPlayerState,
        action: AppAction.superPlayerAction
    ))

    setupView()
    
    // 3
    viewStore.send(.loadVideo)
}

func handleVideo(store: Store
   ) {
    
   // 4

       
   let superPlayer 
   = 
   SuperPlayerViewController(
   store: store)
    superPlayer.
   view.
   frame 
   = view.
   frame
    superPlayer.
   view.
   backgroundColor 
   = .
   black

    
   addChild(superPlayer)
    view.
   addSubview(superPlayer.
   view)
    superPlayer.
   didMove(
   toParent: 
   self)
}
  
  1. Setup store and viewStore to pass states as objects to interact with view.
  2. Scope to transform a store into SuperPlayer’s store that deals with local state and actions.
  3. Send loadVideo action load the video.
  4. Update SuperPlayerViewController parameter to use the store that was scoped before.

Build the project, and it will show you the video player with url we provide to environment. Now we can play video without setting up many AVPlayer states.

Data Flow

1_63q5BLtn9DCl6fQcjfpMrw

Requirements

The SuperPlayer depends on the Combine framework and ComposableArchitecture, so it requires minimum deployment targets of iOS 13

Installation

You can add SuperPlayer to an Xcode project by adding it as a package dependency.

  1. From the File menu, select Swift Packages › Add Package Dependency…
  2. Enter https://github.com/tokopedia/ios-superplayer into the package repository URL text field
  3. Depending on how your project is structured:
    • If you have a single application target that needs access to the library, then add SuperPlayer directly to your application.
    • If you want to use this library from multiple Xcode targets, or mixing Xcode targets and SPM targets, you must create a shared framework that depends on SuperPlayer and then depend on that framework in all of your targets.

Help

If you want to discuss the SuperPlayer or have a question about how to use it to solve your video player problem, you can start a topic in the discussions tab of this repo

Credits and thanks

These following people is working hard to craft this lovely player and its documentations to make what SuperPlayer is today:

Adityo Rancaka, Alvin Matthew Pratama, and many of iOS Superman, and iOS Tokopedia Team ❤️

License

This library is released under the MIT license. See LICENSE for details.

You might also like...
An advanced media player library, simple and reliable
An advanced media player library, simple and reliable

About The SRG Media Player library provides a simple way to add universal audio / video playback support to any application. It provides: A controller

Swift library for embedding and controlling YouTube videos in your iOS applications via WKWebView!

YouTubePlayer Embed and control YouTube videos in your iOS applications! Neato, right? Let's see how it works. 0.7.0 Update: WKWebView breaking change

A Swift library to upload video files to api.video platform.
A Swift library to upload video files to api.video platform.

api.video IOS video uploader api.video is the video infrastructure for product builders. Lightning fast video APIs for integrating, scaling, and manag

A high-performance, flexible, and easy-to-use Video compressor library written by Swift.

FYVideoCompressor A high-performance, flexible and easy to use Video compressor library written by Swift. Using hardware-accelerator APIs in AVFoundat

Superhero-composable-watch-example - A superhero themed experiment using the swift composable architecture on a SwiftUI/Combine iOS app and Watch extension A Flutter plugin to wrap HyperPay SDK for iOS and Android.
A Flutter plugin to wrap HyperPay SDK for iOS and Android.

HyperPay Flutter Plugin This plugin is a wrapper around HyperPay iOS and Android SDK, it's still in alpha release, and supports limited set of functio

A custom layout built on top of SwiftUI's Layout API that lays elements out in multiple lines. Similar to flex-wrap in CSS, CollectionViewFlowLayout.
A custom layout built on top of SwiftUI's Layout API that lays elements out in multiple lines. Similar to flex-wrap in CSS, CollectionViewFlowLayout.

WrapLayout A custom layout built on top of SwiftUI's Layout API that lays elements out in multiple lines. Similar to flex-wrap in CSS, CollectionViewF

A library to derive and compose Environment's in The Composable Architecture.

ComposableEnvironment This library brings an API similar to SwiftUI's Environment to derive and compose Environment's in The Composable Architecture.

A library that provides undo semantics for the Composable Architecture with optional bridging tofUndoManager.

Swift Composable Undo A library that provides undo semantics for the Composable Architecture with optional bridging with UndoManager. Motivation It is

Learn how to structure your iOS App with declarative state changes using Point-Free's The Composable Architecture (TCA) library.
Learn how to structure your iOS App with declarative state changes using Point-Free's The Composable Architecture (TCA) library.

Learn how to structure your iOS App with declarative state changes using Point-Free's The Composable Architecture (TCA) library.

Open source game built in SwiftUI and the Composable Architecture.
Open source game built in SwiftUI and the Composable Architecture.

isowords This repo contains the full source code for isowords, an iOS word search game played on a vanishing cube. Connect touching letters to form wo

Powerful navigation in the Composable Architecture via the coordinator pattern

TCACoordinators The coordinator pattern in the Composable Architecture TCACoordinators brings a flexible approach to navigation in SwiftUI using the C

A powerful, minimal and composable architecture for building reactive iOS apps with SwiftUI or UIKit

SourceArchitecture A simple yet powerful framework for reactive programming with only a minimal optimized set of types. Sources are self-contained, hi

Pointfree's Composable Architecture Relay

RelayStore Pointfree's Composable Architecture Relay to hook into Actions sent to the Store from outside. Read more at Observe actions in The Composab

Modules to use with The Composable Architecture

TCA-Modules Modules to use with The Composable Architecture You can run Examples

ScrumdingerTCA - Apple’s tutorial app recreated using The Composable Architecture
ScrumdingerTCA - Apple’s tutorial app recreated using The Composable Architecture

ScrumdingerTCA Apple’s tutorial app recreated using The Composable Architecture

AudioPlayer is syntax and feature sugar over AVPlayer. It plays your audio files (local & remote).

AudioPlayer AudioPlayer is a wrapper around AVPlayer. It also offers cool features such as: Quality control based on number of interruption (buffering

ModernAVPlayer is a persistence AVPlayer wrapper

ModernAVPlayer is a persistence AVPlayer wrapper ++ Cool features ++ Get 9 nice and relevant player states (playing, buffering, loading

Comments
  • Create Swift Package

    Create Swift Package

    Create Swift Package for SuperPlayer

    STR

    • Create new xcode project
    • Add SuperPlayer SwiftPackageManager ( https://github.com/tokopedia/ios-superplayer.git )
    • Chose branch rule, and type "develop" Screen Shot 2021-08-26 at 12 52 52
    • To test the demo can copy code from this ViewController
    • Don't forget to allow arbitrary loads from Info.plist because our example using HTTP. Screen Shot 2021-08-27 at 15 37 47

    Expected

    https://user-images.githubusercontent.com/85599884/131108786-457fd9d3-1808-4c46-b710-09750a63dda0.mp4

    opened by alvinmatthewp 0
Owner
Tokopedia
Tokopedia is one of the fastest growing internet companies in Indonesia, enabling individuals to sell and buy online for free.
Tokopedia
Player View is a delegated view using AVPlayer of Swift

PlayerView [![CI Status](http://img.shields.io/travis/David Alejandro/PlayerView.svg?style=flat)](https://travis-ci.org/David Alejandro/PlayerView) An

null 131 Oct 25, 2022
Swifty360Player - iOS 360-degree video player streaming from an AVPlayer.

Swifty360Player iOS 360-degree video player streaming from an AVPlayer. Demo Requirements Swifty360Player Version Minimum iOS Target Swift Version 0.2

Abdullah Selek 148 Dec 18, 2022
Gumlet analytics integration with AVPlayer for iOS native applications.

gumlet-Insights-avplayer Gumlet Insights integration with AVPlayer for iOS native applications. This Insights enables you to get useful data about vid

Gumlet 0 Dec 15, 2021
VIMVideoPlayer is a simple wrapper around the AVPlayer and AVPlayerLayer classes.

VIMVideoPlayer is a simple wrapper around the AVPlayer and AVPlayerLayer classes.

Vimeo 280 May 11, 2022
NYT360Video plays 360-degree video streamed from an AVPlayer on iOS.

NYT360Video 360º video playback from The New York Times NYT360Video plays spherical 360º video, allowing the user to explore the video via pan gesture

The New York Times 270 Nov 23, 2022
Pretty iOS mobile screens + AVPlayer video view ––– made in SwiftUI

UrbanVillageProjectScreens Recreated UI screens from the conceptual Urban Village Project. Read more about the project here. Please open an issue if y

10011.co 23 Dec 29, 2022
NYT360Video plays 360-degree video streamed from an AVPlayer on iOS.

NYT360Video 360º video playback from The New York Times NYT360Video plays spherical 360º video, allowing the user to explore the video via pan gesture

The New York Times 270 Nov 23, 2022
Collection of Swift/iOS-related conference videos. A demo project for SuperArc framework - building modular iOS apps with a µComponent architecture.

SwiftCommunity Beta version is available at TestFlight Collection of Swift/iOS-related conference videos. This project serves as a showcase for the Su

SuperArcSwift 52 Dec 2, 2022
Camera and Microphone streaming library via RTMP, HLS for iOS, macOS, tvOS.

HaishinKit (formerly lf) Camera and Microphone streaming library via RTMP, HLS for iOS, macOS, tvOS. Issuesの言語は、日本語が分かる方は日本語でお願いします! Sponsored with ??

shogo4405 2.4k Dec 29, 2022
NextLevelSessionExporter is an export and transcode media library for iOS written in Swift.

NextLevelSessionExporter ?? NextLevelSessionExporter is an export and transcode media library for iOS written in Swift. The library provides customiza

NextLevel 233 Nov 27, 2022