swift 中未播放 Mp3 音频
Mp3 audio is not playing in swift
这是我的swiftclass
import Foundation
import AVFoundation
class MusicPlayer{
static let shared = MusicPlayer()
var audioPlayer: AVAudioPlayer?
func startBackgroundMusic(backgroundMusicFileName: String) {
if let bundle = Bundle.main.path(forResource: backgroundMusicFileName, ofType: "mp3") {
let backgroundMusic = NSURL(fileURLWithPath: bundle)
do {
audioPlayer = try AVAudioPlayer(contentsOf:backgroundMusic as URL)
guard let audioPlayer = audioPlayer else { return }
audioPlayer.numberOfLoops = -1
audioPlayer.volume = 1.0
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
}
func stopBackgroundMusic() {
guard let audioPlayer = audioPlayer else { return }
audioPlayer.stop()
}
}
而且,这是我的控制器代码 class
func downloadUsingAlamofire() {
let audioUrl = URL(string:"https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba.mp3")
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(
audioUrl!,
method: .get,
parameters: nil,
encoding: URLEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: { (progress) in
}).response(completionHandler: { (DefaultDownloadResponse) in
let imageURL = fetchPathinDirectory(filepath:audioUrl!.lastPathComponent)
MusicPlayer.shared.startBackgroundMusic(backgroundMusicFileName: imageURL.path)
})
}
如评论中所述;
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
这意味着该文件已下载到您应用程序的文档目录中。但是当你阅读文件时:
if let bundle = Bundle.main.path(forResource: backgroundMusicFileName, ofType: "mp3") {
let backgroundMusic = NSURL(fileURLWithPath: bundle)
您在应用程序包中读取文件,这是您的应用程序及其资源(故事板、xib 等)的安装位置。
您必须从文档目录中读取文件。像 :
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let downloadedFileUrl = documentsURL.appendingPathComponent(backgroundMusicFileName)
这是我的swiftclass
import Foundation
import AVFoundation
class MusicPlayer{
static let shared = MusicPlayer()
var audioPlayer: AVAudioPlayer?
func startBackgroundMusic(backgroundMusicFileName: String) {
if let bundle = Bundle.main.path(forResource: backgroundMusicFileName, ofType: "mp3") {
let backgroundMusic = NSURL(fileURLWithPath: bundle)
do {
audioPlayer = try AVAudioPlayer(contentsOf:backgroundMusic as URL)
guard let audioPlayer = audioPlayer else { return }
audioPlayer.numberOfLoops = -1
audioPlayer.volume = 1.0
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
}
func stopBackgroundMusic() {
guard let audioPlayer = audioPlayer else { return }
audioPlayer.stop()
}
}
而且,这是我的控制器代码 class
func downloadUsingAlamofire() {
let audioUrl = URL(string:"https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba.mp3")
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(
audioUrl!,
method: .get,
parameters: nil,
encoding: URLEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: { (progress) in
}).response(completionHandler: { (DefaultDownloadResponse) in
let imageURL = fetchPathinDirectory(filepath:audioUrl!.lastPathComponent)
MusicPlayer.shared.startBackgroundMusic(backgroundMusicFileName: imageURL.path)
})
}
如评论中所述;
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
这意味着该文件已下载到您应用程序的文档目录中。但是当你阅读文件时:
if let bundle = Bundle.main.path(forResource: backgroundMusicFileName, ofType: "mp3") {
let backgroundMusic = NSURL(fileURLWithPath: bundle)
您在应用程序包中读取文件,这是您的应用程序及其资源(故事板、xib 等)的安装位置。 您必须从文档目录中读取文件。像 :
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let downloadedFileUrl = documentsURL.appendingPathComponent(backgroundMusicFileName)