iphone - Swift AVAudioplayer on Button press -


i trying have button play sound when pushed.

the app have has 6 buttons (with 6 different sounds). have 1 listed because trying 1 button work correctly before doing rest. know button working (from print command @ bottom), not playing sound.

import uikit import avfoundation  class viewcontroller: uiviewcontroller {      override func viewdidload() {         super.viewdidload()         // additional setup after loading view, typically nib.     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }      var player : avaudioplayer?      @ibaction func buttonone(_ sender: any) {         func playsound() {             let url = bundle.main.url(forresource: "drinking", withextension: "mp3")!              {                 player = try avaudioplayer(contentsof: url)                 guard let player = player else { return }                  player.preparetoplay()                 player.play()             } catch let error nserror {                 print(error.description)             }         }         print("anakin: working!")     } } 

is printing error?

can try doing this?

let path = bundle.main.path(forresource: "file name extension", oftype:nil)! let url = url(fileurlwithpath: path)  {     let sound = try avaudioplayer(contentsof: url)     sound.play() } catch {     // couldn't load file :( } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -