gpt4 book ai didi

ios - 无法在我的音乐播放器应用程序上播放音乐

转载 作者:行者123 更新时间:2023-11-30 12:24:34 25 4
gpt4 key购买 nike

我正在尝试为我的学校项目制作一个音乐播放器应用程序。

当我运行该应用程序时,一切正常,除了当我单击歌曲时它不播放。

请帮我提供详细的解释,以便我修复该应用程序。

代码如下:

import UIKit
import AVFoundation

var songs:[String] = []
var audioPlayer = AVAudioPlayer()

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var myTabelView: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return songs.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = songs[indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{

do
{
if let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3") {
try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath) as URL)
}
} catch {
print(error.localizedDescription)
}
}



override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

gettingSongName()
}

override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



func gettingSongName() // Get the names of all the songs
{
let folderUrl = URL(fileURLWithPath: Bundle.main.resourcePath!)

do
{
let songPath = try FileManager.default.contentsOfDirectory(at: folderUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) // Axcess all of the song files

for song in songPath
{
var mySong = song.absoluteString


if mySong.contains(".mp3")
{
let findString = mySong.components(separatedBy: "/")
mySong = (findString[findString.count-1])
mySong = mySong.replacingOccurrences(of: "%20", with: " ")
mySong = mySong.replacingOccurrences(of: ".mp3", with: " ")
songs.append(mySong)

}
}

myTabelView.reloadData()
}
catch
{

}

}


}

最佳答案

您只是初始化audioPlayer而不要求它播放,请尝试在audioPlayer初始化之后将其添加到didSelectRow do block 中

audioPlayer.prepareToPlay()
audioPlayer.volume = 1.0
audioPlayer.play()
audioPlayer.delegate = self

关于ios - 无法在我的音乐播放器应用程序上播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44359975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com