我正在尝试制作一个小应用程序来快速教自己一些我在解决如何使我的应用程序以某种方式运行时遇到一些问题。
我的应用程序应该能够播放一个无声的声音,就像它在这个视频中的声音一样......
https://www.youtube.com/watch?v=Ks5bzvT-D6I
但每次重复点击屏幕时,声音播放前都会有一点延迟,所以听起来根本就不是这样。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var hornSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("horn", ofType: "mp3")!)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: hornSound, error: &error)
audioPlayer.prepareToPlay()
}
@IBAction func playSound(sender: UIButton) {
audioPlayer.pause()
audioPlayer.currentTime = 0
audioPlayer.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我也遇到过关于使用 spritekit 的这个帖子
并且试着让它在没有延迟的情况下播放声音,但是使用精灵套件我无法阻止现有声音,因此它们只是重叠而不是我想要的效果。
是否有解决方案让它按照视频中的声音工作。
1 回答
Apple 建议使用
AVAudioPlayer
来播放音频数据,除非您需要非常低的 I/O 延迟。所以你可能想尝试另一种方法。
在我的一个应用程序中,我通过从我的 wav 文件创建系统声音 ID 来播放倒计时声音。在课堂上试试这个: