I want to play sound not from the beginning of the mp3 file, but from 14s after beginning. I have the following code for sound playing, but i was not successful in finding the solution:
if let textAudio = textAudio {
let wait = SKAction.wait(forDuration: 0.1)
let play = SKAction.play()
if view.scene! == scene02 {
// play 14s after begining
}
textAudio.run(SKAction.sequence([wait, play]))
}
You can use AVAudioPlayer and the currentTime method as explained here:
import AVFoundation
var textAudio: AVAudioPlayer?
let path = Bundle.main.path(forResource: "example.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
textAudio = try AVAudioPlayer(contentsOf: url)
textAudio?.currentTime = TimeInterval(14.0)
textAudio?.play()
} catch {
// couldn't load file :(
}
Related
I'm trying to play a file I downloaded from S3, after locating the file and passing the URL to the audio player, the audio file won't play/is missing.
This is the complete fileURL: file:///var/mobile/Containers/Data/Application/61F2FC20-4C62-4263-B147-0010805BC0FA/Documents/Dump%20Trucks.mp3
Here is my code:
func playAudio(){
var soundClip: AVAudioPlayer?
if let directory = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first{
let path = NSURL(fileURLWithPath: directory).appendingPathComponent("Dump Trucks.mp3")
print("The path is \(String(describing: path))")
do {
soundClip = try AVAudioPlayer(contentsOf: path!)
soundClip?.play()
} catch {
print("Error: Audio File missing.")
}
}
}
The problem is that your player is a local variable. Thus your method comes to an end and the player is destroyed before it ever has a chance to start playing! Declare it as an instance property instead.
So
func playAudio(){
var soundClip: AVAudioPlayer?
Becomes
var soundClip: AVAudioPlayer?
func playAudio(){
This might not solve all your problems but if you don’t do it you certainly will never hear sound.
import Foundation
import AVFoundation
final class MediaPlayer {
static var player = AVAudioPlayer()
class func play() {
do {
let file = Bundle.main.url(forResource: "file_name", withExtension: "mp3")!
player = try AVAudioPlayer(contentsOf: file)
player.numberOfLoops = 0 // loop count, set -1 for infinite
player.volume = 1
player.prepareToPlay()
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
try AVAudioSession.sharedInstance().setActive(true)
player.play()
} catch _ {
print("catch")
}
}
}
I have had success playing sounds by pressing a button, by using the following code. However, I'd like to press a button and have that sound play in a loop/infinitely. How is this acheived? I'd also like to acheive pause/play functionality as well. Thanks in advance.
#IBAction func keyPressed(_ sender: UIB`utton) {
playSound(soundName: sender.currentTitle!)
}
func playSound(soundName: String) { //
let url = Bundle.main.url(forResource: soundName, withExtension: "wav")
player = try! AVAudioPlayer(contentsOf: url!)
player.play()
} // End of Play Sound
By default AVAudioPlayer plays its audio from start to finish then stops, but we can control how many times to make it loop by setting its numberOfLoops property. For example, to make your audio play three times in total, you’d write this:
player.numberOfLoops = 3
if you want the infinite loop then use
player.numberOfLoops = -1
for e.g
func playSound(soundName: String) { //
let url = Bundle.main.url(forResource: soundName, withExtension: "wav")
player = try! AVAudioPlayer(contentsOf: url!)
player.numberOfLoops = -1 // set your count here
player.play()
} // End of Play Sound
var audioPlayer: AVAudioPlayer?
func startBackgroundMusic() {
if let bundle = Bundle.main.path(forResource: "Guru_Nanak_Sahib_Ji", ofType: "mp3") {
let backgroundMusic = NSURL(fileURLWithPath: bundle)
do {
audioPlayer = try AVAudioPlayer(contentsOf:backgroundMusic as URL)
guard let audioPlayer = audioPlayer else { return }
audioPlayer.numberOfLoops = -1 // for infinite times
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
}
You can retain the player. Then in the player's completion delegate callback, start playing again. Or stop the player at any time, since you've retained a reference to it.
Is there any way to play audio file and vibrate iphone when audio is started and stop vibrating when audio stops playing?
currently this code is working but it plays audio and when audio stops then it vibrates the phone, I want both (audio and vibration) play together.
var alertSoundEffect: AVAudioPlayer?
let path = Bundle.main.path(forResource: "alert.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
alertSoundEffect = try AVAudioPlayer(contentsOf: url)
alertSoundEffect?.prepareToPlay()
alertSoundEffect?.play()
} catch {
return
}
let vib = SystemSoundID(1519)
AudioServicesPlayAlertSound(SystemSoundID(vib))
AudioServicesRemoveSystemSoundCompletion(vib)
You can try
DispatchQueue.global().async
{
var alertSoundEffect: AVAudioPlayer?
let path = Bundle.main.path(forResource: "alert.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
alertSoundEffect = try AVAudioPlayer(contentsOf: url)
alertSoundEffect?.prepareToPlay()
alertSoundEffect?.play()
} catch {
return
}
}
So basically I have two audio players while using the AVFoundation. I want the second one to be played right after the first one finishes.
myFilePathString = NSBundle.mainBundle().pathForResource("\(workout[currentExercise])", ofType: "mp3")
#IBAction func startWorkout(sender: AnyObject) {
if let myFilePathString = myFilePathString {
let myFilePathURL = NSURL(fileURLWithPath: myFilePathString)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: myFilePathURL)
audioPlayer.play()
}catch {
print("Error")
}
}
This above one is to be the first
let path = NSBundle.mainBundle().pathForResource("Rest", ofType: "mp3")
let url = NSURL(fileURLWithPath: path!)
do {
let sound = try AVAudioPlayer(contentsOfURL: url)
secondAudioPlayer = sound
sound.play()
} catch {
print("error")
}
...while this one is to be played after. At the moment they play at the same time. Got any ideas for me?
Thank you in advance.
I have two functions. First one loops list of names for audio to be played, and the second one is the actual player function. The problem is that it plays only the last one. It does not play one by one waiting till the next is finished. How it possible to perform in a sequence? I tried to add: dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), { }) ... still does not work. Any possible solution. Thank you in advance!
func startGame(){
audioPlay("NowListen")
for i in 0...self.soundArray.count - 1 {
print(self.soundArray[i])
self.audioPlay(self.soundArray[i])
}
}
func audioPlay(name: String){
var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("\(name)", ofType: "mp3")!)
print(alertSound)
// try to play sound
do{
self.audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
} catch {
}
}
Use AVQueuePlayer ( init it with Items at the start and it will play in queue, one after another ) class instead of AVAudioPlayer.
In your Start Game, you can do following:
var audioItems: [AVPlayerItem] = []
for audioName in soundsArray {
let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(audioName, ofType: "mp3")!)
let item = AVPlayerItem(URL: url)
audioItems.append(item)
}
let player = AVQueuePlayer(items: audioItems)
player.play()