当从 uitableviewcell 按下 UIButton 时,如果有任何一个从其他单元格播放,则停止其他声音
Stop Others Sound if any one playing from other cells when UIButton is press from uitableviewcell
当我按下 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
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
}
}
}
不要每次都检查是否播放音频。只需调用 player.pause()
然后使用 player.play()
开始播放音频
不要在 RadioCollectionViewCell
中初始化播放器,它会创建您的播放器的多个实例。因此,仅在 viewDidLoad
中初始化 viewcontroller 中的播放器一次,并使用同一播放器播放来自不同单元格的不同 url 的音乐。所以你的单元格应该只有 URl(不同的 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
中创建新播放器。使用同一个播放器。您可以使用一个声明为 publicly 的标准播放器。不要在 ibaction 中创建新的。获取 avplayer
的 public 实例并使用相同的实例来播放音乐。而已。
希望这会有所帮助:)
尝试将速率默认设置为 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方法中设置rate默认为0.0,然后只在播放声音文件时赋值1.0。
希望这有效!
当我按下 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
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
}
}
}
不要每次都检查是否播放音频。只需调用 player.pause()
然后使用 player.play()
不要在 RadioCollectionViewCell
中初始化播放器,它会创建您的播放器的多个实例。因此,仅在 viewDidLoad
中初始化 viewcontroller 中的播放器一次,并使用同一播放器播放来自不同单元格的不同 url 的音乐。所以你的单元格应该只有 URl(不同的 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
中创建新播放器。使用同一个播放器。您可以使用一个声明为 publicly 的标准播放器。不要在 ibaction 中创建新的。获取 avplayer
的 public 实例并使用相同的实例来播放音乐。而已。
希望这会有所帮助:)
尝试将速率默认设置为 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方法中设置rate默认为0.0,然后只在播放声音文件时赋值1.0。
希望这有效!