When I pressed on UIButton
Music starts. And when I again press on button music will stop. During playing music if I press on another cell button both music plays. I didn't understand how I can stop music that is playing from other cells or in other words only one music instance playing at once. Also a button image animation automatically set to its default state like sender.selected = false
Please can anyone tell me how i can do this?
Thanks
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
}
}
In my 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
}
}
}
Don't check every time audio is playing or not. Simply call player.pause()
then start audio playing with player.play()