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

标签 ios swift swift3 avfoundation

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

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

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

代码如下:

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/

相关文章:

iphone - 'right to left' 书面语言的 UITextView 对齐

ios - Realm :更新对象而不更新列表

audio - 在单元格上重复的按钮图像

ios - 如果使用核心数据,应用程序会崩溃

swift:如何使用 mongoDB Realm 数据库和身份验证保持 ios 用户登录

ios - 在 UIView 中正确显示谷歌地图

swift - guard 语句中的多个 let 是否与单个 let 相同?

javascript - 谷歌地图登录混合应用卡住了

ios - 检查用户位置是否靠近某些点

ios - 在 iOS 9 中隐藏自定义键盘扩展上方的工具栏