【问题标题】:Swift: Sound plays on simulator but not on deviceSwift:声音在模拟器上播放,但不在设备上
【发布时间】:2023-03-22 07:39:01
【问题描述】:

我正在为 iPhone/iPad 构建一个应用程序,我正在尝试解决一个声音问题:

该应用程序带有一个四分钟的倒数计时器。当倒计时到达 00:00 时,会触发短暂的音效。这在不同的模拟器上运行良好,但我的手机保持静音。该应用程序是为 7.1 构建的。我想知道这是因为代码还是我的手机无法正常工作(确实有声音问题)。我的代码看起来还好吗?我希望有人可以帮助解决这个问题。

这里是:

import Foundation
import UIKit
import AVFoundation


class VC11 : UIViewController {


    @IBOutlet weak var timerLabel: UILabel!



    var timer = NSTimer()
    var count = 240
    var timerRunning = false
    var audioPlayer = AVAudioPlayer()


    override func viewDidLoad() {
        super.viewDidLoad()


            func nextPage(sender:UISwipeGestureRecognizer) {
                switch sender.direction {

                case UISwipeGestureRecognizerDirection.Left:
                    print("SWIPED LEFT")
                    self.performSegueWithIdentifier("seg11", sender: nil)
                default:
                    break

                }


                var leftSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
                var rightSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))

                leftSwipe.direction = .Left
                rightSwipe.direction = .Right

                view.addGestureRecognizer(leftSwipe)
                view.addGestureRecognizer(rightSwipe)
            }


        }



    func updateTime() {
        count--

            let seconds = count % 60
            let minutes = (count / 60) % 60
            let hours = count / 3600
            let strHours = hours > 9 ? String(hours) : "0" + String(hours)
            let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
            let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
            if hours > 0 {
                timerLabel.text = "\(strHours):\(strMinutes):\(strSeconds)"
            }
            else {
                timerLabel.text = "\(strMinutes):\(strSeconds)"

        }
        stopTimer()


           }
  @IBAction func startTimer(sender: AnyObject) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

    sender.setTitle("Running...", forState: .Normal)

       }


    func stopTimer()

    {

        if count == 0 {
            timer.invalidate()
            timerRunning = false
            timerLabel.text = "04:00"
            playSound()
            count = 240

        }


    }




    func playSound() {

    var soundPath = NSBundle.mainBundle().pathForResource("Metal_Gong", ofType: "wav")
    var soundURL = NSURL.fileURLWithPath(soundPath!)


        self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
        self.audioPlayer.play()
    }


}

【问题讨论】:

    标签: ios swift audio


    【解决方案1】:

    确保您的设备未触发进入静音模式。

    此外,对于简短的系统声音,您可以使用 AudioToolbox (https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/) 中的系统声音:

    if let soundURL = NSBundle.mainBundle().URLForResource("Metal_Gong", withExtension: "wav") {
        var mySound: SystemSoundID = 0
        AudioServicesCreateSystemSoundID(soundURL, &mySound)
        AudioServicesPlaySystemSound(mySound);
    }
    

    【讨论】:

    • 感谢您的链接和代码。我在哪里添加代码?现在我添加它来覆盖 func viewDidLoad() { super.viewDidLoad() 但它也不起作用。
    • 尝试将它添加到几个函数中,比如在 playSound() 中,可惜没有运气。
    • 尝试在updateTime()函数中调试。在count-- 行之后通过NSLog 打印count 变量。可能是计时器出了问题,而不是声音。
    • 我在 count 之后写了 println("count")——它确实打印了四次 count
    • 只有四个?它必须显示 240 条日志,每秒一条(从 239 到 0)
    【解决方案2】:

    我刚刚发现我的代码确实有效 - 只是因为我的手机坏了,所以没有声音播放。感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 2015-01-27
      • 1970-01-01
      • 2021-07-19
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多