ios - 为什么 AVPlayer 不播放任何内容?

标签 ios swift parse-platform

我有一个播放按钮,当它被点击时,它会播放解析中的歌曲。该程序找到了歌曲并获取了链接,我在 Debug模式下进行了检查,但由于某种原因它没有播放这首歌。我在 native 音乐应用程序中播放了一首歌曲,当我单击应用程序中的播放按钮时,它会停止在后台播放音乐。我不认为这是一个编程错误。为什么它不起作用?

  func playit(sender: UIButton!){
    let cell: UITableViewCell = self.table.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

    let playButtonrow = sender.tag

    println(titleatcell[playButtonrow])



    if let nowPlaying = musicPlayer.nowPlayingItem{
    let title = nowPlaying[MPMediaItemPropertyTitle] as? String
    let artist = nowPlaying[MPMediaItemPropertyTitle] as? String



    println("now playing \(title!) \(artist!)")
    println("cell: \(playButtonrow) \(titleatcell[playButtonrow])")

        let query = PFQuery(className: "Songs")
        query.whereKey("SongName", equalTo: titleatcell[playButtonrow])
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) song(s).")
                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        println(object.objectId)

                        println(playButtonrow)
                        let object = object as PFObject
                        let parseAudio = object.valueForKey("SongFile") as! PFFile
                        let audioPath: String = parseAudio.url!
                        let urlParse: NSURL = NSURL(string: audioPath)!

                        player = AVPlayer(URL: urlParse)
                        println(player)
                        player.volume = 1.0
                        player.play()
                        if (player.rate > 0) && (player.error == nil) {
                            // player is playing
                            println("Playing")
                        } else {
                            println("Not Playing")
                        }


                    }
                }
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }

        }


    }

}

最佳答案

您需要将AVPlayer 代码放在主线程中。将代码包装在 dispatch_async 中:

    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) song(s).")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                for object in objects {
                    println(object.objectId)

                    println(playButtonrow)
                    let object = object as PFObject
                    let parseAudio = object.valueForKey("SongFile") as! PFFile
                    let audioPath: String = parseAudio.url!
                    let urlParse: NSURL = NSURL(string: audioPath)!

                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        player = AVPlayer(URL: urlParse)
                        println(player)
                        player.volume = 1.0
                        player.play()
                        if (player.rate > 0) && (player.error == nil) {
                            // player is playing
                            println("Playing")
                        } else {
                            println("Not Playing")
                        }
                    })
                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }

关于ios - 为什么 AVPlayer 不播放任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686701/

相关文章:

ios - 应用程序:didFinishLaunchingWithOptions: method getting called on app crash

ios - 未调用 CLLocation 管理器 didStartMonitoringForRegion 委托(delegate)方法

ios - 连接两列的值并将其与 sqlite 中的一个值进行比较

ios - 在 .xib 中的 UICollectionViewCell 中使用 IBOutlet 时出现 NSUnknownKeyException

ios - NSPropertyListSerialization 无法为 TableView 中的详细信息 View 设置图像字符串 (iOS)

arrays - 我需要创建一个 UIImages 数组,我在 Swift 中从相机胶卷加载这些图像

ios - 一个 View Controller 中的两个 TableView ,一个 TableView 未显示

ios - PFQueryTableViewController 中带有图像的动态单元格高度

javascript - parse.com JavaScript 应用程序。如何将其他用户角色更新为管理员

ios - 从 Parse 下载后使用图像或文本进行排序