Swift:声音在模拟器上播放,但不在设备上播放 [英] Swift: Sound plays on simulator but not on device

查看:114
本文介绍了Swift:声音在模拟器上播放,但不在设备上播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为iPhone/iPad开发一个应用,并且正在尝试解决声音问题:

I'm building an app for iPhone/iPad, and I'm trying to solve a sound problem:

该应用程序附带一个四分钟的倒数计时器.当coundown到达00:00时,会触发短促的音效.在不同的模拟器上都可以正常工作,但我的手机保持静音.该应用程序适用于7.1.我想知道是由于代码原因还是我的手机无法正常工作(确实有声音问题).我的代码看起来还好吗?我希望有人可以提供帮助.

The app comes with a four minute countdown timer. When the coundown reaches 00:00, it triggers a short sound effect. This works fine on the different simulators, but my phone remains silent. The app is built for 7.1. I'm wondering if it's because of the code or if my phone isn't working (it does have sound issues). Does my code look okay? I hope someone can help with this.

这里是:

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()
    }


}

推荐答案

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

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

Make sure that your device is not triggered into silent mode.

Also, for short system sounds you can use System Sounds from 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);
}

这篇关于Swift:声音在模拟器上播放,但不在设备上播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆