platform : ios, '13.0 '
use_frameworks!
! [ 请添加图片描述] ( https: // img- blog. csdnimg. cn/ 109b01990d9a41319c1431be5af88bc6. png)
target 'DemoApp ' do
source 'https: // github. com/ CocoaPods / Specs . git'
pod 'AudioPlayerSwift '
end
import UIKit
import AudioPlayer
class ViewController : UIViewController {
var audioPlayer : AudioPlayer !
override func viewDidLoad ( ) {
super . viewDidLoad ( )
yeTye ( )
}
func yeTye ( ) {
do {
try audioPlayer = AudioPlayer ( fileName: "music.mp3" )
audioPlayer. completionHandler = { ( _ didFinish: Bool ) -> Void in
print ( "--- 已完成播放 或 已停止播放,The music finishes playing, or is stopped." )
}
} catch {
print ( "---Sound initialization failed" )
}
}
func yeTyeMusicPlay ( ) {
audioPlayer. volume = 0.8
audioPlayer. numberOfLoops = 3
audioPlayer. play ( )
}
func yeTyeMusicStop ( ) {
if audioPlayer. isPlaying {
print ( "总时长,audioPlayer.duration: \( audioPlayer. duration ) " )
print ( "当前时刻, audioPlayer.currentTime: \( audioPlayer. currentTime ) " )
print ( "名字,audioPlayer.name: \( audioPlayer. name ?? "" ) " )
print ( "完整路径,audioPlayer.url: \( audioPlayer. url! ) " )
audioPlayer. stop ( )
}
}
}