用于 Swift 项目的音频播放器
CocoaPods 是 Cocoa 项目的依赖管理工具。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 MLAudioPlayer 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
pod 'MLAudioPlayer', '~> 1.2.1'
然后,运行以下命令
$ pod install
Carthage 是一个去中心化的依赖管理工具,可自动将框架添加到您的 Cocoa 应用程序。
您可以使用 Homebrew 通过以下命令安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将 MLAudioPlayer 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "micheltlutz/MLAudioPlayer" ~> 1.2.1
要将 MLAudioPlayer 用作 Swift Package Manager 包,只需在您的 Package.swift 文件中添加以下内容。
// 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"])
]
)
如果您不想使用上述任何依赖管理工具,您可以手动将 MLAudioPlayer 集成到您的项目中。
cd
到您的顶层项目目录,并运行以下命令(如果您的项目尚未初始化为 git 仓库)$ git init
$ git submodule add https://github.com/micheltlutz/MLAudioPlayer.git
$ git submodule update --init --recursive
打开新的 MLAudioPlayer
文件夹,并将 MLAudioPlayer.xcodeproj
拖到您的应用程序 Xcode 项目的项目导航器中。
它应该出现在您的应用程序蓝色项目图标下。它在所有其他 Xcode 组之上还是之下并不重要。
在项目导航器中选择 MLAudioPlayer.xcodeproj
并验证部署目标是否与您的应用程序目标匹配。
接下来,在项目导航器中选择您的应用程序项目(蓝色项目图标)以导航到目标配置窗口,并在侧边栏的“Targets”标题下选择应用程序目标。
在该窗口顶部的选项卡栏中,打开“General”面板。
单击“Embedded Binaries”部分下的 +
按钮。
您将看到两个不同的 MLAudioPlayer.xcodeproj
文件夹,每个文件夹都包含两个不同版本的 MLAudioPlayer.framework
,嵌套在 Products
文件夹中。
从哪个
Products
文件夹中选择并不重要。
选择 MLAudioPlayer.framework
。
就是这样!
MLAudioPlayer.framework
会自动添加为目标依赖项、链接框架和嵌入框架,并通过复制文件构建阶段完成,这就是您在模拟器和设备上构建所需的一切。
+
按钮。MLAudioPlayer.framework
。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"
}
在某些情况下,需要延迟加载音频,直到例如完成某些其他屏幕加载。 在这些情况下,可以以这种方式配置播放器。
//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])
}
在 viewWillAppear
中播放器开始加载
您可以在 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
}
使用方法
NotificationCenter.default.post(name: Notification.Name.MLAudioPlayerNotification,
object: nil,
userInfo: ["action":MLPlayerActions.stop])
MLAudioPlayer 的可用操作
- play
- pause
- stop
- reset
var config = MLPlayerConfig()
config. imageNamePlayButton = "customPlayButton"
在此项目中更改 MLAudioPlayerDemo Build 的目标并运行
参见 文档
MLAudioPlayer 文档 (已记录 46%)
欢迎提出问题和拉取请求!
Michel Anderson Lutz Teixeira @michel_lutz
MLAudioPlayer 在 MIT 许可证下发布。 有关详细信息,请参见 LICENSE。