**Try this code ...**
Declare one variable for get index.
var songnamearray : NSArray = NSArray()
var audioPlayer = AVAudioPlayer()
var selectedindex : Int?
var isFristtime : Bool = false
override func viewDidLoad() {
super.viewDidLoad()
songnamearray = ["Saiyaan (Sanam) 320Kbps(MajMasti.in)" ,"Phone Mein Teri Photo (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ,"Mile Ho Tum Humko (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ," Rain Mashup (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ,"Car Mein Music Baja (Neha Kakkar) 320Kbps"]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return songnamearray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Songcell", forIndexPath: indexPath)
// info 5 play 6 name 7
let eventname: UILabel = (cell.viewWithTag(7) as! UILabel)
let info: UIButton = (cell.viewWithTag(5) as! UIButton)
let play: UIButton = (cell.viewWithTag(6) as! UIButton)
if selectedindex == indexPath.row{
if isFristtime == true
{
play.setImage(UIImage(named: "pause-button.png"), forState: UIControlState.Normal)
}else{
play.setImage(UIImage(named: "play-button.png"), forState: UIControlState.Normal)
}
}else{
play.setImage(UIImage(named: "play-button.png"), forState: UIControlState.Normal)
}
info.setImage(UIImage(named: "icon (5).png"), forState: UIControlState.Normal)
play.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown)
info.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown)
eventname.text = songnamearray.objectAtIndex(indexPath.row) as? String
return cell
}
@IBAction func CloseMethod(sender: UIButton, event: AnyObject) {
let touches = event.allTouches()!
let touch = touches.first!
let currentTouchPosition = touch.locationInView(self.Songlisttable)
let indexPath = self.Songlisttable.indexPathForRowAtPoint(currentTouchPosition)!
selectedindex = indexPath.row
if isFristtime == true
{
audioPlayer.pause()
isFristtime = false
}else{
let songnamestring : NSString = songnamearray.objectAtIndex(indexPath.row) as! String
do {
if let bundle = NSBundle.mainBundle().pathForResource(songnamestring as String, ofType: "mp3") {
let alertSound = NSURL(fileURLWithPath: bundle)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
} catch {
print(error)
}
isFristtime = true
}
self.Songlisttable.reloadData()
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…