[英]Why is the AVPlayer not playing anything?
I have a play button and when it is clicked it will play the song in parse. 我有一个播放按钮,当单击它时,它将解析播放歌曲。 The program finds the song and gets the link, I checked in debug mode, but for some reason it's not playing the song.
我在调试模式下检查了程序找到的歌曲并获取了链接,但是由于某种原因,它没有播放歌曲。 I played a song in the native music app, and when I click the play button in the app it stops the music playing in the background.
我在本地音乐应用程序中播放了歌曲,然后在应用程序中单击播放按钮时,它将停止在后台播放音乐。 I don't think it's a programming bug.
我不认为这是编程错误。 Why is it not working?
为什么不起作用?
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!)")
}
}
}
}
You need to put the AVPlayer
code on the main thread. 您需要将
AVPlayer
代码放在主线程上。 Wrap the code in dispatch_async
: 将代码包装在
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!)")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.