Home>
I ’m making a wake-up app, I ’m a swift beginner

I want to be able to set it after checking the volume of the alarm with the slider.
I saw the code itself while looking at the site, but the mp3 file does not sound even if I tamper with the slider.
Please tell me if it is good. Thanking you in advance.

Applicable source code
import UIKit
import AVFoundation
class FirstSwitchViewController: UIViewController {
    var audioPlayer: AVAudioPlayer!
    var volumeSlider = UISlider ()
    @IBOutlet weak var label: UILabel!
    @IBAction func back (_ sender: UIBarButtonItem)
    {
        self.dismiss (animated: true, completion: nil)
    }
    @IBAction func volumeChange (_ sender: UISlider)
    {
        let value = round (sender.value * 100)/1
        volumeSlider.value = sender.value
        label.text = "\ (value)"
        audioPlayer.volume = volumeSlider.value
    }
    override func viewDidLoad () {
        super.viewDidLoad ()
        if let url = Bundle.main.url (forResource: "2", withExtension: ". mp3") {
            do {
                audioPlayer = try AVAudioPlayer (contentsOf: url)
                audioPlayer? .play (atTime: 1 * 10)
            } catch {
                audioPlayer = nil
            }
        } else {
            fatalError ("Url is nil")
        }
    }
}
Tried

http://bakapro.seesaa.net/article/429338314.html
I used this site as a reference

  • Answer # 1

    I didn't read the question carefully and answered it, but when I tweaked the volume with the slider,
    Is it okay to check how much volume you want to play without pressing the play button?

    If playback is possible,
    Set @ IBAction's connection destination to UISlider to valueChanged (is it the default?)

    If you play it, will it make a sound when the value changes (probably when you speak your finger from the round part of the slider)?

    @ IBAction func volumeChange (_ sender: UISlider)
        {
            let value = round (sender.value * 100)/1
            volumeSlider.value = sender.value
            label.text = "\ (value)"
            audioPlayer.volume = volumeSlider.value
            // Just play it. Doesn't it make a sound?
            audioPlayer.play ()
        }