我正在为 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()
}
}
确保您的设备未触发进入静音模式。
此外,对于简短的系统声音,您可以使用 AudioToolbox 中的系统声音(https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/ 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)