MLAudioPlayer
Swift 项目用 AudioPlayer
要求
- iOS 10.0+
- Xcode 10.0+
安装
依赖管理器
CocoaPods
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
Carthage 是一个去中心化的依赖管理器,它自动化了将框架添加到您的 Cocoa 应用程序的过程。
您可以使用以下命令使用 Homebrew 安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将 MLAudioPlayer 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "micheltlutz/MLAudioPlayer" ~> 1.2.1
Swift 包管理器
要将 MLAudioPlayer 作为 Swift 包管理器 包使用,只需在您的 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 集成到项目中。
Git 子模块
- 打开终端,
cd
到您的顶级项目目录,并运行以下命令(如果您的项目尚未初始化为 Git 仓库)
$ git init
- 通过运行以下命令将 MLAudioPlayer 添加为 git 子模块
$ git submodule add https://github.com/micheltlutz/MLAudioPlayer.git
$ git submodule update --init --recursive
-
打开新的
MLAudioPlayer
文件夹,并将MLAudioPlayer.xcodeproj
拖放到您的应用程序的 Xcode 项目的项目导航器中。它应该出现在您的应用程序蓝色项目图标下方的嵌套位置。它是在所有其他 Xcode 组之上还是之下,并不重要。
-
在项目导航器中选择
MLAudioPlayer.xcodeproj
,并验证部署目标是否与您的应用程序目标相匹配。 -
接下来,在项目导航器(蓝色项目图标)中选择您的应用程序项目,进入目标配置窗口并选择侧边栏中“目标”标题下的应用程序目标。
-
在窗口顶部的标签栏中,打开“通用”面板。
-
在“嵌入式二进制文件”部分下单击
+
按钮。 -
您将看到两个不同的
MLAudioPlayer.xcodeproj
文件夹,每个文件夹中都有一个MLAudioPlayer.framework
的不同版本,嵌套在Products
文件夹中。您可以选择哪个
Products
文件夹并不重要。 -
选择
MLAudioPlayer.framework
。 -
到此为止!
MLAudioPlayer.framework
将自动添加为目标依赖项,在复制文件构建阶段链接框架和嵌入框架,这是您在模拟器和设备上构建所需的全部。
嵌入式二进制文件
- 从 https://github.com/micheltlutz/MLAudioPlayer/releases 下载最新版本
- 接下来,在项目导航器(蓝色项目图标)中选择您的应用程序项目,进入目标配置窗口并选择侧边栏中“目标”标题下的应用程序目标。
- 在窗口顶部的标签栏中,打开“通用”面板。
- 在“嵌入式二进制文件”部分下单击
+
按钮。 - 添加下载的
MLAudioPlayer.framework
。 - 到此为止!
使用方法
您需要在您的 Assets 中有带有指定名称的这些图像文件
- 播放
- 暂停
- 刷新
- playerLoad
- trackTracking
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
是否可以更改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 构建和运行目标
文档
查看 文档
MLAudioPlayer 文档(46% 已文档化)
做出贡献
欢迎提交问题和拉取请求!
待办事项
- 播放本地文件(感谢 @maclacerda)
- 支持通知中心以停止后台音频
- 迁移到 Swift 4.2(感谢 @maclacerda)
- 迁移到 Swift 5
- 实现 MPRemoteCommandCenter
- 带有封面图片的音频播放器类型
- 100% 已文档化
- HealthKit 的回调
作者
Michel Anderson Lutz Teixeira @michel_lutz
贡献
许可协议
MLAudioPlayer 在 MIT 许可协议下发布。有关详细信息,请参阅 LICENSE。