一、界面展示
二、代码结构
三、详细代码
MusicUtils
//
// MusicUtils.swift
// MusicDemo
//
// Created by iscopy on 2022/2/16.
//
import Foundation
//音频框架
import AVFoundation
class MusicUtils: NSObject {
fileprivate static var player : AVAudioPlayer?
}
// MARK: - 对歌曲的控制
extension MusicUtils: AVAudioPlayerDelegate{
/// 播放本地音乐
class func playLocationMusic(_ musicName : String){
//获取资源的URL
guard let url = Bundle.main.url(forResource: musicName, withExtension: nil) else { return }
//判断和暂停/停止的对象是否同一首歌曲(继续播放的时候不会切歌)
if player?.url == url{
player?.play()
return
}
//根据URL创建AVAudioPlayer对象
guard let audioPlayer = try? AVAudioPlayer(contentsOf: url) else { return }
self.player = audioPlayer
//播放音乐
audioPlayer.play()
}
/// 播放网络音乐
class func playNetworkMusic(_ urlString : String){
//获取资源的URL
guard let url = URL.init(string: urlString) else { return }
//判断和暂停/停止的对象是否同一首歌曲(继续播放的时候不会切歌)
if player?.url == url{
player?.play()
return
}
guard let urlData = try? Data(contentsOf: url) else { return }
//根据URL创建AVAudioPlayer对象
guard let audioPlayer = try? AVAudioPlayer(data: urlData) else { return }
self.player = audioPlayer
//播放音乐
audioPlayer.play()
}
class func pauseMusic() {
player?.pause()
}
class func stopMusic(){
player?.stop()
player?.currentTime = 0
}
}
// MARK: - 对其他的控制(音量/时间/代理设置)
extension MusicUtils{
class func changeVolume(volume : Float){
player?.volume = volume
}
class func setCurrentTime(_ currentTime : TimeInterval) {
player?.currentTime = currentTime
}
class func getCurrentTime() -> TimeInterval{
return player?.currentTime ?? 0
}
class func getDuration() -> TimeInterval{
return player?.duration ?? 0
}
class func setPlayerDelegate(_ delegate : AVAudioPlayerDelegate){
player?.delegate = delegate
}
}
ViewController
//
// ViewController.swift
// MusicDemo
//
// Created by iscopy on 2022/2/15.
//
import UIKit
//首先导入音频框架
import AVFoundation
class ViewController: UIViewController {
//MARK: - 名称
@IBOutlet weak var tvMusicName: UILabel!
//MARK: - 开始
@IBAction func btnStart(_ sender: Any) {
tvMusicName.text = self.music[self.postion]
print("\(self.postion) \(self.music[self.postion])")
//调用本地音乐播放
MusicUtils.playLocationMusic(self.music[self.postion])
}
//MARK: - 暂停
@IBAction func btnSuspend(_ sender: Any) {
MusicUtils.pauseMusic()
}
//MARK: - 上一曲
@IBAction func btnLastSong(_ sender: Any) {
if self.postion == 0{
self.postion = self.music.count-1
}else{
self.postion = self.postion-1
}
tvMusicName.text = self.music[self.postion]
print("\(self.postion) \(self.music[self.postion])")
MusicUtils.playLocationMusic(self.music[self.postion])
}
//MARK: - 下一曲
@IBAction func btnNextSong(_ sender: Any) {
if self.postion == self.music.count-1{
self.postion = 0
}else{
self.postion = self.postion+1
}
tvMusicName.text = self.music[self.postion]
print("\(self.postion) \(self.music[self.postion])")
MusicUtils.playLocationMusic(self.music[self.postion])
}
var postion:Int = 0
var music:Array = [String]()
//MARK: - viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
music.append("等什么君 - 岁月神偷(翻自 金玟岐).mp3")
music.append("多多poi - 02的爱恋(Feat.约谈人参)(翻自 约谈人参).mp3")
music.append("排骨教主 - 牵丝戏.mp3")
}
}
注:网络音乐播放请调用 playNetworkMusic 方法,这里只是调用了本地播放
GitHub - iscopy/MusicDemo: swift 本地音乐播放
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)