Hello,
was hoping for some help for a small app I’m practising with ?
I have searched and searched and watched numerous “play sound youtubes” dont seem to be able to crack it ?
The App segues to another view controller and plays the sound track straight after the segue.
This works fine and plays sound in my simulator, but as soon as i try it on my device it segues to the View Controller but no sound plays ? Its as if the volume setting on the device is set to zero although I have checked all i can ? ?
Would love some help please if any suggesions ?
‘’’
import UIKit
import AVFoundation
class GoTourViewController: UIViewController, AVAudioPlayerDelegate {
var player: AVAudioPlayer?
@IBOutlet weak var volumeSlider: UISlider!
@IBAction func changeVolume(_ sender: UISlider) {
player?.setVolume(sender.value, fadeDuration: 0.1)
}
@IBAction func actionButtons(_ sender: UIButton) {
let index = sender.tag
switch index {
case 0:
player?.play()
case 1:
player?.pause()
case 2:
player?.currentTime = 0
player?.stop()
default:
break
}
}
override func viewDidLoad() {
super.viewDidLoad()
if let player = player, player.isPlaying {
// Stop playback
player.stop()
}
else {
// set up player and play
let urlString = Bundle.main.path(forResource: "Track", ofType: "mp3")
do {
try AVAudioSession.sharedInstance().setMode(.default)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
guard let urlString = urlString else {
return
}
player = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: urlString))
guard let player = player else {
return
}
// player.prepareToPlay()
print("prepare to play called")
player.play()
//TODO: - On completion of playing audio track on player go back to locationViewController ?
}
catch {
print("Something went wrong")
}
}
}
}
‘’’