ios - How to load and play a list of audio files -
i have 2 view controllers collectionviewchild
, imageviewcontroller
. load image in imageviewcontroller
. on image have button want play specific sound on each image. know how play single audio sound don't know how select specific sound , play required image.
// collectionviewchild , here want display image in collectionviewchild func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) { self.performseguewithidentifier("showdetailedimage", sender: self) } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?){ if segue.identifier == "showdetailedimage" { let indexpaths = self.collectionviewchild!.indexpathsforselecteditems()! let indexpath = indexpaths[0] nsindexpath let vc = segue.destinationviewcontroller as! imageviewcontroller vc.title = self.tabledata[indexpath.row] vc.image = images[indexpath.row] as! uiimage }}}
// imageviewcontroller
import uikit import avfoundation class imageviewcontroller: uiviewcontroller { @iboutlet weak var imageview03: uiimageview! var selmake = uiimage() var image = uiimage() var audioplayer = avaudioplayer() // on button want play specific audio file @ibaction func soundbutton(sender: anyobject) { let soundlocation = nsbundle.mainbundle().pathforresource("2", oftype: ".mp3") { self.audioplayer = try avaudioplayer(contentsofurl: nsurl(fileurlwithpath: soundlocation!)) try avaudiosession.sharedinstance().setcategory(avaudiosessioncategoryplayback) try avaudiosession.sharedinstance().setactive(true) } catch { print("something bad happened. try catching specific errors narrow things down") } self.audioplayer.preparetoplay() self.audioplayer.play() } else { print("") } override func viewdidload() { self.imageview03.image = self.image super.viewdidload() }
would helper class multiple audio players not help? made simple audio player class that's extendable multiple tracks.
you can helper github https://github.com/crashoverride777/swift2-music-helper
when image pressed play corresponding sound in button method.
music.sharedinstance.playimage1() music.sharedinstance.playimage2()
...
you combine mohameds comment above.
is looking for?
Comments
Post a Comment