I need to playback a single audio file with TheAmazingAudioEngine framework and Swift.
I'm completely newbie with this framework and tried the code below, but the audio didn't play. What is wrong? How could I play the audio file?
do {
var audioController = AEAudioController(audioDescription: AEAudioController.nonInterleavedFloatStereoAudioDescription())
let file = NSBundle.mainBundle().URLForResource("myaudiofile", withExtension: "wav")!
let channel = try AEAudioFilePlayer(URL: file)
audioController?.addChannels([channel])
channel.playAtTime(0)
} catch {
print("Failure")
}
I missed to start the audioController:
do {
var audioController = AEAudioController(audioDescription: AEAudioController.nonInterleavedFloatStereoAudioDescription())
let file = NSBundle.mainBundle().URLForResource("myaudiofile", withExtension: "wav")!
let channel = try AEAudioFilePlayer(URL: file)
audioController?.addChannels([channel])
try audioController!.start()
} catch {
print("Failure")
}
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 am using MusicPlayer & MusicSequence for play .mid file. i want to get Audio duration from .mid file, set Playback Speed on .mid file. i am using below code for play .mid file
var musicPlayer: MusicPlayer?
var sequence: MusicSequence?
func play(file: String) {
guard let midiFile = Bundle.main.url(forResource: file, withExtension: "mid") else {
return
}
NewMusicPlayer(&musicPlayer)
NewMusicSequence(&sequence)
if let musicPlayer = musicPlayer, let sequence = sequence {
MusicSequenceFileLoad(sequence, midiFile as CFURL, .midiType, MusicSequenceLoadFlags())
MusicPlayerSetSequence(musicPlayer, sequence)
MusicPlayerStart(musicPlayer)
}
}
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
}
}
I want to make piano player in which I want to play predefined notes which are some .mid files. Here is my code which is not working.
let soundPath: String? = Bundle.main.path(forResource: "0_100", ofType: "mid")
let midiFile:URL = URL(fileURLWithPath: soundPath ?? "")
var midiPlayer: AVMIDIPlayer?
do {
try midiPlayer = AVMIDIPlayer(contentsOf: midiFile, soundBankURL: nil)
midiPlayer?.prepareToPlay()
midiPlayer?.play {
print("finished playing")
}
} catch {
print("could not create MIDI player")
}
soundBankURL is missing in the following:
try midiPlayer = AVMIDIPlayer(contentsOf: midiFile, soundBankURL: nil)
It's required according to the doc: https://developer.apple.com/documentation/avfoundation/avmidiplayer/1389225-init
Important
For macOS the bankURL can be set to nil to use the default sound bank. However, iOS must always refer to a valid bank file.
A soundBank can be a .sf2 file for instance.
I have 2 variant of audio file, first embedded, second which I download from server.
First varian is work well.
But when I get audio file from server is not playing and get error.
I get path:
file:///var/mobile/Containers/Data/Application/24656EA9-6F1F-4C0F-A6FE-5D457908A80D/Documents/Dumont.mp3
And trying to play
func playMusic(filename: String) {
let url = NSBundle.mainBundle().URLForResource(filename, withExtension: "mp3")
guard let newURL = url else {
print("Could not find file: \(filename)")
return
}
do {
player = try AVAudioPlayer(contentsOfURL: newURL)
player!.numberOfLoops = -1
player!.prepareToPlay()
player!.play()
} catch let error as NSError {
print(error.description)
}
}
How to play this audio file in swift 2.3?
Platform iOS >8., Swift 2.3
Hey stupid guys who downvote answer for you:
if let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path = NSURL(fileURLWithPath: dir).URLByAppendingPathComponent("Dumont.mp3")
do {
player = try AVAudioPlayer(contentsOfURL: path)
player!.numberOfLoops = -1
player!.prepareToPlay()
player!.play()
} catch let error as NSError {
print(error.description)
}
This code play downloaded song!