当我按下UIButton
时,音乐开始。当我再次按下按钮时,音乐将停止。在播放音乐的过程中,如果我按下另一个单元格按钮,两个音乐都会播放。我不明白如何停止其他单元格播放的音乐,或者换句话说,一次只能播放一个音乐示例。还有一个按钮图像动画自动设置为默认状态,如sender.selected = false
请问有谁能告诉我我该怎么做?
谢谢
UIButton connected with CollectionViewCell Class
Default image State And when i pressed on button on different cells each cell play music and and change its image state to play
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
}
}
}
3条答案
按热度按时间eqqqjvef1#
不要每次都检查音频是否在播放,只需调用
player.pause()
,然后使用player.play()
启动音频播放kfgdxczn2#
不要在
RadioCollectionViewCell
中初始化播放器,它将创建多个播放器示例。因此,在viewDidLoad
中viewcontroller中只初始化一次播放器,并使用同一个播放器播放来自不同单元格的具有不同url的音乐。因此,您的单元格应该只有URl(不同的音乐有不同的url),实现**didSelectItemAtIndexPath
**方法,通过该方法将url传递给普通播放器,并通过该方法播放。按备注中的要求更新:
例如,在一个实施例中,
在这里,您可以使用url初始化播放器。当选择单元格时,将调用此方法。因此,在单击单元格时初始化播放器。如果您有
array of url
,则可以通过indexPath获取特定的url。因此,从该方法初始化播放器和播放。编辑:
如果你在单元格中使用按钮来播放和暂停,那么就不要实现上面的代码。
我发现你正在
IBaction
中创建新的播放器。使用相同的播放器。你可以使用一个公开声明的标准播放器。不要在ibaction中创建新的。取avplayer
的公共示例并使用相同的播放器播放音乐。就是这样。希望这会有所帮助:)i2byvkas3#
尝试将速率默认设置为0.0,并仅在弹奏时指定1.0。
也可以在didSelect方法中设置rate,默认为0.0,然后只在播放声音文件时指定1.0。
希望这能奏效!