Play sound with a little delay
You can try using this:
let seconds = 1.0//Time To Delay
let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
//Play Sound here
})
Full code:
func playAudioWithDelay()
{
let file = NSBundle.mainBundle().URLForResource("PR1", withExtension: "wav")
player = AVAudioPlayer(contentsOfURL: file, error: nil)
player!.volume = 0.5
player!.numberOfLoops = -1
player!.playAtTime(//I tried with 0.5 but doesn't work)
player!.prepareToPlay()
let seconds = 1.0//Time To Delay
let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
player!.play()
})
}
Playing a sound from library causing little delay Actionscript 3.0
Your test measurements are inaccurate. Flash Sounds have a high latency for over 50ms to 300ms depending on the system. As such they are not usable for what you are trying to do. You need to use ane or any external sound playing system to achieve accuracy (low latency). There is no known way to reduce latency on Flash Sound, the sampledataevent was introduced in FP 10 to give one way for developer to synchronize sounds but that system is not meant for low latency responsive sound (sampledataevent can go up to 1000ms latency).
Bottom line, using Flash Sound API, there's no way to synchronize the playing of sounds accurately due to their high latency. There's no work around and no possibility to reduce that latency to the lowest industry standard (30ms). What you are trying to achieve cannot sadly be done in Flash.
How to play music a little bit delayed with a real-time Advanced audio analyzer
Assuming you can access the Web Audio API via the framework, you could use the createDelay()
to create a delay node. The delay is given in seconds.
Then simply:
- Plug source into analyzer node as well as delay node.
- Then connect delay node to destination
- Process the data from analyzer node as usual.
Play sound effect without latency
Apple recommends AVAudioPlayer
for playback of audio data unless you require very low I/O latency.
So you might want to try another approach.
In one of my apps I play countdown sounds by creating a system sound ID from my wav file. Try this in your class:
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)
}
...
}
How can I delay an alert and reload so an audio file can play in javascript?
If you want to add a delay between playing the sound and the alert, you can do it by two ways:
Note that
.play()
ing a sound is asynchronous: the code will inmediately keep executing even if the audio hasn't started playing yet, that's why you need to either wait for the sound to end (recommended, in my opinion), or usesetTimeout
onended
audio event
var lives = -1;var monsterHP = -1;
function gameReset() { //If the player has lost if (lives < 0) { gameOverSound = new Audio('https://soundbible.com/grab.php?id=2167&type=mp3'); gameOverSound.play(); gameOverSound.onended = function() { alert("You lost. Click 'Ok' to restart the game"); location.reload(); } }}
function winCondition() { //If the player has won if (monsterHP < 0) { monsterDeathSound = new Audio('https://soundbible.com/grab.php?id=2182&type=mp3'); //Assigns the monster's death sound monsterDeathSound.play(); monsterDeathSound.onended = function() { alert("Congratulations you beat the monster! Hit 'OK' to restart the game."); location.reload(); } }}
<button onclick="gameReset()">reset</button><button onclick="winCondition()">win</button>
Related Topics
Toggle Sidebar in Swiftui Navigationview on MACos
Troubles Using Cgpathcontainspoint Swift
API Violation - Multiple Calls Made to -[Xctestexpectation Fulfill]
How to Select All Text in a Uitextfield Using Swift
What Does "Arg = Exploded" Mean in Swift Crash Log
Running Swift Build in Terminal Leading to "Platform Path" Errors
Swift, Error Exc_Breakpoint (Code=1, Subcode=0X100695474)
How to Open Your App's Settings (Inside the Settings App) with Swift (iOS 11)
Why Are Properties of an Immutable Object Mutable in Swift
How to Detect Text View Begin Editing and End Editing in Swift 3
Behaviour of Protocols with Self
How to Implement a Generic Struct That Manages Key-Value Pairs for Userdefaults in Swift
Why Does This Swiftui Picker Code Not Work
Swiftui Multiline Text Background Color
How to Make Deinit Take Effect in Swift