0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Swift5 カウントダウン後、効果音が鳴り止まない

Posted at

はじめに

カウントダウンタイマーを作成している時に、終了したら効果音を鳴らすコードを書いていました。
効果音はAudioToolboxを使用しました。
カウントダウンが終了し効果音を鳴らしました。
そうしたところ効果音が止まらなくなり、鳴り続けるという現象が起きました。
凄く初歩的なミスでしたが、効果音を鳴らすための処理に原因があると考えていたため凄く調べるのに時間を取られてしまいました。
そこで何が原因かを調べたので、忘却録として書こうと思いました。

環境

Xcode12.3
Swift5.3.2
macOS Catalina 10.15.4

AudioToolboxを使う準備

こちらを参考にして準備します。

カウントダウン終了時の画面

カウントダウン終了で終わらないで、カウントダウンがプリントされ続けると同時に効果音が鳴り続いてしまいました。
スクリーンショット 2021-01-30 18.30.04.png
※全コードは下の方にあります。

原因

凄く初歩的なミスで、タイマーを破棄しなかった事でした。

ViewContlloer.swift
  timer.invalidate()

原因を探す時に役立った事

teratailさんの質問用テンプレートに書き込みながら、頭を整理していく事で原因部分を特定する事が出来ました。

今回コードを書いている時は、効果音を鳴らす事を目的にしていました。
そのため、効果音を鳴らすためのコードに原因があると思い長い時間調べました。

AudioServicesPlaySystemSound(soundID)//これが原因と仮説

そのためTimerに原因があるとは思いませんでした。
質問テンプレートに書いていると問題が解決するという事を聞いていましたが、本当にその通りだなと感じました。

これからは、活用していきたいと思います。

効果音が終わらない全コード

ViewContlloer.swift
import UIKit
import AudioToolbox

class ViewController: UIViewController {

    var timerValue = 5

    var timer:Timer = Timer()
    let soundID:SystemSoundID = 1304


    override func viewDidLoad() {
        super.viewDidLoad()

        startTimer()
        // Do any additional setup after loading the view.
    }


    func startTimer() {

        timer.invalidate()

        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.countDwon(timer:)), userInfo: nil, repeats: true)
    }

    @objc func countDwon(timer: Timer) {

        timerValue -= 1

        if(timerValue > 0) {

            print(timerValue)
        } else {
            AudioServicesPlaySystemSound(soundID)
            print("カウントダウン終了")
        }
    }
}

効果音が正しくなるようにした全コード

ViewContlloer.swift
import UIKit
import AudioToolbox

class ViewController: UIViewController {

    var timerValue = 5

    var timer:Timer = Timer()
    let soundID:SystemSoundID = 1000


    override func viewDidLoad() {
        super.viewDidLoad()

        startTimer()
        // Do any additional setup after loading the view.
    }


    func startTimer() {

        timer.invalidate()

        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.countDwon(timer:)), userInfo: nil, repeats: true)
    }

    @objc func countDwon(timer: Timer) {

        timerValue -= 1

        if(timerValue > 0) {

            print(timerValue)
        } else {
            AudioServicesPlaySystemSound(soundID)

            //タイマーを破棄
            //ここを書かないと繰り返しAudioServicesPlaySystemSound(soundID)が呼ばれる
            timer.invalidate()
            print("カウントダウン終了")
        }
    }
}

終わりに

これから、上手く動かない時は質問テンプレートを活用していきたいと思います。

もし間違いがあれば優しく教えていただけると嬉しいです。

参照サイト

https://github.com/TUNER88/iOSSystemSoundsLibrary
https://teratail.com/questions/input
https://qiita.com/KikurageChan/items/5b33f95cbec9e0d8a05f

Twitterやってます。
Twitter

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?