Jor*_*dan 3 audio avfoundation avaudioplayer ios swift
我正在尝试制作一个小应用程序来快速教自己一些我在解决如何使我的应用程序以某种方式运行时遇到一些问题.
我的应用程序应该能够播放一个无声的声音,就像它在这个视频中的声音一样......
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.
}
}
Run Code Online (Sandbox Code Playgroud)
我也遇到过关于使用spritekit的这个帖子
并且试着让它在没有延迟的情况下播放声音,但是使用精灵套件我无法阻止现有声音,所以它们只是重叠而不是我想要的效果.
是否有解决方案让它按照视频中的声音工作.
Apple建议AVAudioPlayer播放音频数据,除非您需要非常低的I/O延迟.
所以你可能想尝试另一种方法.
在我的一个应用程序中,我通过从我的wav文件创建系统声音ID来播放倒计时声音.在课堂上试试这个:
import UIKit
import AudioToolbox
class ViewController: UIViewController {
var sound: SystemSoundID = 0
override func viewDidLoad() {
super.viewDidLoad()
var hornSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("horn", ofType: "mp3")!)
AudioServicesCreateSystemSoundID(hornSound!, &self.sound)
}
@IBAction func playSound(sender: UIButton) {
AudioServicesPlaySystemSound(self.sound)
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1415 次 |
| 最近记录: |