当我按下UIButton
音乐开始时。当我再次按下按钮音乐将停止。在播放音乐期间,如果我按下另一个单元格按钮,则播放两个音我不知道如何阻止从其他单元格播放的音乐,或者换句话说,只有一个音乐实例一次播放。此外,按钮图像动画会自动设置为默认状态,如sender.selected = false
任何人都可以告诉我怎么做到这一点?
由于
UIButton connected with CollectionViewCell Class
class RadioCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
let reuseIdentifier = "cell"
var objects = [
["url" : "https://s3.amazonaws.com/kargopolov/BlueCafe.mp3", "image": "1.jpg"],
["url" : "https://s3.amazonaws.com/kargopolov/BlueCafe.mp3", "image": "2.jpg"],
["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/02%20-%20Kheech%20Meri%20Photo%20-%20DownloadMing.SE.mp3", "image": "3.jpg"],
["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/03%20-%20Bewajah%20-%20DownloadMing.SE.mp3", "image": "4.jpg"],
["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/04%20-%20Tera%20Chehra%20-%20DownloadMing.SE.mp3", "image": "5.jpg"],
["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/02%20-%20Kheech%20Meri%20Photo%20-%20DownloadMing.SE.mp3", "image": "6.jpg"],
["url" : "http://media.downloadming.se/TEMP/Loveshhuda%20(2015)/01%20-%20Mar%20Jaayen%20-%20DownloadMing.SE.mp3", "image": "5.jpg"],
["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/01%20-%20Sanam%20Teri%20Kasam%20-%20DownloadMing.SE.mp3", "image": "7.jpg"],
["url" : "http://2016.downloadming1.com/bollywood%20mp3/Sanam%20Teri%20Kasam%20(2016)/02%20-%20Kheech%20Meri%20Photo%20-%20DownloadMing.SE.mp3", "image": "1.jpg"]]
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.objects.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RadioCollectionViewCell
let object = objects[indexPath.row]
cell.url = object["url"]!
cell.img.image = UIImage(named: object["image"]!)
return cell
}
}
在我的UICollectionViewCell
班级
class RadioCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var img: UIImageView!
var url : String!
var playerItem:AVPlayerItem?
var player:AVPlayer?
@IBAction func audioControlButtonAction(sender: UIButton) {
let nurl = NSURL(string: "\(url)")!
playerItem = AVPlayerItem(URL: nurl)
player=AVPlayer(playerItem: playerItem!)
if sender.selected == false {
player!.play()
sender.selected = true
}else
{
player?.pause()
sender.selected = false
}
}
}
答案 0 :(得分:2)
每次播放音频时都不要检查。只需拨打player.pause()
,然后使用player.play()
答案 1 :(得分:0)
不要在RadioCollectionViewCell
中初始化播放器,它会创建播放器的多个实例。所以,只在viewDidLoad
中的viewcontroller中初始化播放器一次并使用同一个播放器播放来自不同小区的不同网址的音乐。因此,您的单元格应该只有URl(不同音乐的不同网址)并实现 didSelectItemAtIndexPath
方法,并通过此方法将url传递给普通玩家并使用此方法播放。
在评论中更新为:
例如,
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
cell.backgroundColor = UIColor.magentaColor()
}
这里你可以用url初始化播放器。选择单元格时会调用此方法。所以在单元格的单击上初始化播放器,如果你有array of url
,那么你可以通过indexPath获得特定的URL。所以初始化玩家并玩这个方法。
修改强>
如果您使用单元格中的按钮进行播放和暂停,则不要执行上述代码。
我发现您正在IBaction
创建新玩家。使用相同的球员。你可以使用一个公开声明的标准玩家。不要在ibaction中创造新的东西。获取avplayer
的公共实例并将其用于播放音乐。那就是它。
希望这会有所帮助:)
答案 2 :(得分:0)
默认情况下,请尝试将设定率设定为0.0,并仅在播放时指定1.0。
@IBAction func audioControlButtonAction(sender:UIButton){
let nurl = NSURL(string: "\(url)")!
playerItem = AVPlayerItem(URL: nurl)
player=AVPlayer(playerItem: playerItem!)
player.rate = 0.0
if sender.selected == false {
player.rate = 1.0
player!.play()
sender.selected = true
}else
{
player?.pause()
sender.selected = false
}
}
或者您可以在didselect方法中设置默认值为0.0,然后仅在您播放时指定1.0。
希望这有效!