Ich versuche eine Musik-Player-App für mein Schulprojekt zu erstellen.Ich kann keine Musik auf meiner Musik-Player-App abspielen
Wenn ich die App läuft alles funktioniert, außer für die Tatsache, dass wenn ich auf einen Song klicken, es nicht spielt.
Bitte helfen Sie mir mit einer detaillierten Erklärung, damit ich die App beheben kann. Hier
ist der Code:
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
{
}
}
}