AudioPlayer for Swift projects
- iOS 10.0+
- Xcode 10.0+
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.2.1'
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.2.1
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.2.1"))
],
targets: [
.target(name: "HelloMLAudioPlayer", dependencies: ["MLAudioPlayer"])
]
)
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 theMLAudioPlayer.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 theMLAudioPlayer.framework
nested inside aProducts
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!
- play
- pause
- refresh
- playerLoad
- thumbTracking
import MLAudioPlayer
//Default Sizes
//MLAudioPlayer.widthPlayerMini = UIScreen.main.bounds.width
//MLAudioPlayer.heightPlayerMini = CGFloat(216)
// For playing stream/online files
var mlAudioPlayer: MLAudioPlayer = {
// For playing the stream/online files
let mlAudioPlayer = MLAudioPlayer(urlAudio: "http://urlyouraudio.mp3")
return mlAudioPlayer
}()
// For playing local storage files
var mlLocalAudioPlayer: MLAudioPlayer = {
// For playing the stream/online files
let mlAudioPlayer = MLAudioPlayer(urlAudio: "file://urlyourlocalaudio.mp3", isLocalFile: true)
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"
}
In some cases it is necessary to delay loading the audio until some other screen loading for example is completed. for these cases it is possible to configure the player in this way.
//set autoload to false
let mlAudioPlayer = MLAudioPlayer(urlAudio: "http://youraudio.mp3",
config: nil,
isLocalFile: false, autoload: false)
/**
Example: Loading after using
Post to .MLAudioPlayerNotification userInfo = ["action": MLPlayerActions.load]
*/
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.post(name: .MLAudioPlayerNotification, object: nil,
userInfo: ["action": MLPlayerActions.load])
}
On viewWillAppear
Player starting load
Can you change any configuration on MLPlayerConfig
class MLAudioPlayer {
/// Define a Title Audio to show in block Screen
public var titleAudio = ""
/// Define a Title for album to show in lock Screen
public var titleAlbum = ""
/// Define a artist name to show in lock Screen
public var artistName = ""
/// Define a artwork to show in block Screen
public var artwork: UIImage?
/// Contains a current time audio
public var currentTime: Double = 0.0
}
//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"
tryAgainFont: UIFont? = UIFont.systemFont(ofSize: 14)
tryAgainColor: UIColor? = UIColor(hex: "246BB3")
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: CGFloat? = UIScreen.main.bounds.width
heightPlayerFull: CGFloat? = 177
widthPlayerMini: CGFloat? = UIScreen.main.bounds.width
heightPlayerMini: CGFloat? = 50
initialVolume: Float? = 0.7
}
Usage:
NotificationCenter.default.post(name: Notification.Name.MLAudioPlayerNotification,
object: nil,
userInfo: ["action":MLPlayerActions.stop])
Available Actions for MLAudioPlayer
- play
- pause
- stop
- reset
var config = MLPlayerConfig()
config. imageNamePlayButton = "customPlayButton"
On this project change target for MLAudioPlayerDemo Build and Run
See Documentation
MLAudioPlayer Docs (46% documented)
Issues and pull requests are welcome!
- Play local files (Thanks @maclacerda)
- Suporte a Notification center to stop background audio
- Migrate to Swift 4.2 (Thanks @maclacerda)
- Migrate to Swift 5
- Implementing MPRemoteCommandCenter
- Player type with cover image for audio
- 100% documented
- Callbacks for HelthKit
Michel Anderson Lutz Teixeira @michel_lutz
MLAudioPlayer is released under the MIT license. See LICENSE for details.