Home

Awesome

ModernAVPlayer

Swift 4.2 Build Status CocoaPods CocoaPods

ModernAVPlayer is a persistence AVPlayer wrapper

++ Cool features ++


Known issue

From version 1.5.1, resume playback from background mode failed. If you have any suggestion, please help.

Use of mixWithOther AVAudiosession CategoryOptions is not a solution.


Menu

Requirements

In order to support background mode, append the following to your Info.plist:

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>

Installation

Swift Package Manager

Supported version: swift-tools-version:5.0

// Package.swift

import PackageDescription

let package = Package(
    name: "Sample",
    dependencies: [
        .package(url: "https://github.com/noreasonprojects/ModernAVPlayer", from: "X.X.X")
    ],
    targets: [
        .target(name: "Sample", dependencies: ["ModernAVPlayer"])
    ]
)

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.3+ is required to build ModernAVPlayer.

To integrate ModernAVPlayer into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'ModernAVPlayer'
end

Then, run the following command:

$ pod install

Getting started

Create media from URL

let media = ModernAVPlayerMedia(url: URL, type: MediaType)

Create media from AVPlayerItem

let media = ModernAVPlayerMediaItem(item: AVPlayerItem, type: MediaType, metadata: PlayerMediaMetadata)

Instanciate the wrapper

let player = ModernAVPlayer()

Load and play the media

player.load(media: media, autostart: true)

Track on repeat

player.loopMode = true
↓ State / Command →loadMediaplaypausestopseek
InitOXOOX
LoadingOXOOX
LoadedOOOOO
BufferingOXOOO
PlayingOXOOO
PausedOOXOO
StoppedOOOXO
WaitingNetworkOXOOX
FailedOOXXX

Advanced

Custom configuration

All player configuration are available from PlayerConfiguration protocol.
A default implementation ModernAVPlayerConfiguration is provided with documentation


Remote command

If using default configuration file ( swift useDefaultRemoteCommand = true), ModernAVPlayer use automatically all commands created by ModernAVPlayerRemoteCommandFactory class Documention available in ModernAVPlayerRemoteCommandFactory.swift file

Custom command

Use your own PlayerConfiguration implementation with

...
useDefaultRemoteCommand = false
...

Create an array of commands conforming to ModernAVPlayerRemoteCommand protocol.

let player = ModernAVPlayer(config: YourConfigImplementation())
let commands: [ModernAVPlayerRemoteCommand] = YourRemoteCommandFactory.commands
player.remoteCommands = commands

You can use existing commands from public ModernAVPlayerRemoteCommandFactory class.


Plugin

Use PlayerPlugin protocol to create your own plugin system, like tracking Plugin.


RxSwift

Instead of using delegate pattern, you can use rx to bind player attributes.

Setup

Use pod 'ModernAVPlayer/RxSwift' in the Podfile

Usage

let player = ModernAVPlayer()
let state: Observable<ModernAVPlayer.State> = player.rx.state

Communication