MLAudioPlayer

Audio Player for Swift projects.

Requirements

  • iOS 10.0+
  • Xcode 9.0+

Installation

Dependency Managers

CocoaPods

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

$ gem install cocoapods

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

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

pod 'MLAudioPlayer', '~> 1.0.5'

Then, run the following command:

$ pod install
Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate MLAudioPlayer into your Xcode project using Carthage, specify it in your Cartfile:

github "micheltlutz/MLAudioPlayer" ~> 1.0.5
Swift Package Manager

To use MLAudioPlayer as a Swift Package Manager package just add the following in your Package.swift file.

// swift-tools-version:4.2

import PackageDescription

let package = Package(
    name: "HelloMLAudioPlayer",
    dependencies: [
        .package(url: "https://github.com/micheltlutz/MLAudioPlayer.git", .upToNextMajor(from: "1.0.5"))
    ],
    targets: [
        .target(name: "HelloMLAudioPlayer", dependencies: ["MLAudioPlayer"])
    ]
)

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate MLAudioPlayer into your project manually.

Git Submodules

  • Open up Terminal, cd into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
  • Add MLAudioPlayer as a git submodule by running the following command:
$ git submodule add https://github.com/micheltlutz/MLAudioPlayer.git
$ git submodule update --init --recursive
  • Open the new MLAudioPlayer folder, and drag the MLAudioPlayer.xcodeproj into the Project Navigator of your application's Xcode project.

    It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.

  • Select the MLAudioPlayer.xcodeproj in the Project Navigator and verify the deployment target matches that of your application target.

  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.

  • In the tab bar at the top of that window, open the "General" panel.

  • Click on the + button under the "Embedded Binaries" section.

  • You will see two different MLAudioPlayer.xcodeproj folders each with two different versions of the MLAudioPlayer.framework nested inside a Products folder.

    It does not matter which Products folder you choose from.

  • Select the MLAudioPlayer.framework.

  • And that's it!

The MLAudioPlayer.framework is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.

Embedded Binaries

  • Download the latest release from https://github.com/micheltlutz/MLAudioPlayer/releases
  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
  • In the tab bar at the top of that window, open the "General" panel.
  • Click on the + button under the "Embedded Binaries" section.
  • Add the downloaded MLAudioPlayer.framework.
  • And that's it!

Usage

You need these images files with named in your Assets

  • play
  • pause
  • refresh
  • playerLoad
  • thumbTracking
import MLAudioPlayer


//Default Sizes
//MLAudioPlayer.widthPlayerMini = UIScreen.main.bounds.width
//MLAudioPlayer.heightPlayerMini = CGFloat(216)
var mlAudioPlayer: MLAudioPlayer = {
    let mlAudioPlayer = MLAudioPlayer(urlAudio: "http://urlyouraudio.mp3")
    return mlAudioPlayer
}()

//Default Sizes
//MLAudioPlayer.widthPlayerFull = UIScreen.main.bounds.width
//MLAudioPlayer.heightPlayerFull = CGFloat(80)
var mlAudioPlayerMini: MLAudioPlayer = {
    var config = MLPlayerConfig()
    config.loadingText = "carregando"
    config.playerType = .mini
    config.tryAgainText = "TENTAR NOVAMENTE"

    let mlAudioPlayerMini = MLAudioPlayer(urlAudio: "http://urlyouraudio.mp3", config: config)
    return mlAudioPlayerMini
}()

//Can you listenign a player heightConstraint changes
mlAudioPlayer.didUpdateHeightConstraint = { constant in
	print("heightConstraint changed"
}

MLPlayerConfig

Can you change any configuration on MLPlayerConfig

See available configurations:

//Default configurations:

MLPlayerConfig {
	labelsColors: UIColor? = UIColor(hex: "5C7A98")
	labelsFont: UIFont? = UIFont.systemFont(ofSize: 14)
	labelsLoadingFont: UIFont? = UIFont.boldSystemFont(ofSize: 14)
   labelsTimerFont: UIFont? = UIFont.systemFont(ofSize: 12)
	playerType: MLPlayerType? = .full
	loadingText: String? = "loading"
	loadErrorText: String? = "Could not load"
	tryAgainText: String? = "TRY AGAIN"
	imageNamePlayButton: String? = "play"
	imageNamePauseButton: String? = "pause"
	imageNameLoading: String? = "playerLoad"
	imageNameTrackingThumb: String? = "thumbTracking"
	trackingTintColor: UIColor? = UIColor(hex: "246BB3")
	trackingMinimumTrackColor: UIColor? = UIColor(hex: "246BB3")
	trackingMaximumTrackColor: UIColor? = UIColor(hex: "B3C4CE")
	progressTintColor: UIColor? = UIColor(hex: "B3C4CE")
	progressTrackTintColor: UIColor? = UIColor(hex: "B3C4CE").withAlphaComponent(0.5)
	widthPlayerFull = widthPlayerFull
	heightPlayerFull = heightPlayerFull
	widthPlayerMini = widthPlayerMini
	heightPlayerMini = heightPlayerMini
}

Using Notification Center

Usage:

NotificationCenter.default.post(name: Notification.Name.MLAudioPlayerNotification, 
										object: nil,
										userInfo: ["action":MLPlayerActions.stop])

Available Actions for MLAudioPlayer

 - play
 - pause
 - stop
 - reset

Can you change the images names

	var config = MLPlayerConfig()
	config. imageNamePlayButton = "customPlayButton"

 

Todo

  • [ ] Player type with cover image for audio
  • [ ] Play local files (currently only url audio files)
  • [x] Suporte a Notification center to stop background audio
  • [x] Migrate to Swift 4.2 (Thanks @maclacerda)
  • [ ] 100% documented

GitHub