我正在研究我的学校项目。
一切都运行正常,但唯一的问题是当我点击一首歌时,这首歌不会播放。
请帮助详细说明,以便我完成应用程序。
我认为问题可能是我得到的警告,但我不确定。
这是警告:
file:///Volumes/SAMAD%20COHEN/iOS/FinalProjectMusicPlayer/FinalProjectMusicPlayer/FirstViewController.swift:warning:缺少文件:/ Volumes / SAMAD COHEN / iOS / FinalProjectMusicPlayer / FinalProjectMusicPlayer / FirstViewController.swift从工作副本中丢失< / p>
以下是代码:
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)
audioPlayer.play()
}
} 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
{
}
}
}