首页 > 解决方案 > 从表格视图中的另一个视图控制器播放某些歌曲时遇到问题

问题描述

我正在尝试制作一个系统,其中有一张包含歌曲列表的专辑,当我按下歌曲时,它会将它带到播放器视图控制器。像 Apple Music 和 Spotify。这些歌曲在我电脑上的一个文件中。它们已经出现在我的 AlbumSongViewController 的列表中,但是当我按下那首歌并且它转到我的 PlayerViewController 时,我不会播放这首歌。任何帮助,我觉得很卡。

我将在我认为有问题的地方显示代码。如果需要,我会添加更多。

专辑歌曲视图控制器:

 var song: [Song] = []

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    retrieveSongs()
}

//Delegate and source for tableview
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SongsTableViewCell", for: indexPath) as! SongsTableViewCell
    cell.mainLabel.text = song[indexPath.row].getName()
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}

播放器视图控制器:

var player: Player!
var song: [Song] = []
let indexPath = IndexPath()


override func viewDidLoad() {
    super.viewDidLoad()
    player = Player()
    setSession()
    UIApplication.shared.beginReceivingRemoteControlEvents()
    becomeFirstResponder()

    NotificationCenter.default.addObserver(self, selector: Selector(("handleInterruption")), name: AVAudioSession.interruptionNotification, object: nil)



     func tableView(_ tableView: AlbumSongViewController, indexPath: IndexPath) {
    player.playStream(fileUrl: "http://127.0.0.1/musicfiles" + song[indexPath.row].getName())
    changePlayButton()
    }
}

标签: iosswift

解决方案


问题

问题是牢房里有空间。例如,trial song.mp3。当它试图从数据库中获取歌曲时,这把事情搞砸了。

解决方案

我忘记了当有空格时它会把事情搞砸。当您将歌曲放入数据库时​​,请记住您应该这样做,例如,trial_song.mp3.


推荐阅读