AKplayer and AKAudioPlayer plug headphones are silent on iPhone6s iPhone8s AKplayer is still playing.
I've tried in many ways, all without success
This my code:
do {
AKSettings.playbackWhileMuted = true;
try AKSettings.setSession(category: .playback, with: .mixWithOthers)
let backFile = Bundle.main.path(forResource: "1", ofType: "mp3")!
let backUrl = URL.init(string: backFile)!
backPlayer = AKPlayer.init(url: backUrl)
AudioKit.output = backPlayer
do {
try AudioKit.start()
} catch {
print("AudioKit.start()失败")
}
} catch {
AKLog("Could not set session category.")
}
Related
I'm hoping someone can help me out here. I'm trying to get a basic recording setup, but AudioKit is crashing on me which I'm sure is my fault and I'm not doing something right. Here's my setup. In my viewDidLoad I'm configuring AudioKit like so:
// Session settings
do {
AKSettings.bufferLength = .short
try AKSettings.setSession(category: .playAndRecord, with: .allowBluetoothA2DP)
} catch {
AKLog("Could not set session category.")
}
AKSettings.defaultToSpeaker = true
// Setup the microphone
micMixer = AKMixer(mic)
micBooster = AKBooster(micMixer)
// Load the test player
let path = Bundle.main.path(forResource: "audio1", ofType: ".wav")
let url = URL(fileURLWithPath: path!)
testPlayer = AKPlayer(url: url)
testPlayer?.isLooping = true
// Setup the test mixer
testMixer = AKMixer(testPlayer)
// Pass the output from the player AND the mic into the recorder
recordingMixer = AKMixer(testMixer, micBooster)
recorder = try? AKNodeRecorder(node: recordingMixer)
// Setup the output mixer - only pass the player NOT the mic
outputMixer = AKMixer(testMixer)
// Pass the output mixer to AudioKit
AudioKit.output = outputMixer
// Start AudioKit
do {
try AudioKit.start()
} catch {
print("AudioKit did not start! \(error)")
}
The app builds fine, but as soon as I trigger recorder.record(), the app crashes with the message:
required condition is false: mixingDest.
I really don't want to be passing the mic to the speaker output, but I DO want to be recording it.
I want to be able to play a file back via the "testPlayer", hear it through the speaker, and simultaneously be able to record the output from the "testPlayer" and the mic, without passing the mic back out through the speakers.
I'm sure this is doable but I don't know enough about how these things should work to know what I'm doing wrong.
Any help hugely appreciated!!
Ok so after talking with the AudioKit guys I found out the issue was because my recordingMixer was not connected to AudioKit.output. In order to pull audio through a mixer, it needs to be connected to AudioKit.output.
In order to mute the output from my recordingMixer, I had to create a dummy recording mixer that was muted, and passed that through to the outputMixer.
See the updated example below:
// Session settings
do {
AKSettings.bufferLength = .short
try AKSettings.setSession(category: .playAndRecord, with: .allowBluetoothA2DP)
} catch {
AKLog("Could not set session category.")
}
AKSettings.defaultToSpeaker = true
// Setup the microphone
micMixer = AKMixer(mic)
micBooster = AKBooster(micMixer)
// Load the test player
let path = Bundle.main.path(forResource: "audio1", ofType: ".wav")
let url = URL(fileURLWithPath: path!)
testPlayer = AKPlayer(url: url)
testPlayer?.isLooping = true
// Setup the test mixer
testMixer = AKMixer(testPlayer)
// Pass the output from the player AND the mic into the recorder
recordingMixer = AKMixer(testMixer, micBooster)
recorder = try? AKNodeRecorder(node: recordingMixer)
// Create a muted mixer for recording audio, so AudioKit will pull audio through the recording Mixer, but not play it through the output.
recordingDummyMixer = AKMixer(recordingMixer)
recordingDummyMixer.volume = 0
outputMixer = AKMixer(testMixer, recordingDummyMixer)
// Pass the output mixer to AudioKit
AudioKit.output = outputMixer
// Start AudioKit
do {
try AudioKit.start()
} catch {
print("AudioKit did not start! \(error)")
}
I hope this helps someone in the future.
I'm using Xcode 7.3.1 with iOS 9 and having issue with playing sound by AVAudioPlayer. I've tried all possible solution out there and nothing works Here is my code
let mp3Url = NSBundle.mainBundle().URLForResource("BCA", withExtension:"mp3")!
var player = AVAudioPlayer()
do {
player = try AVAudioPlayer(contentsOfURL: mp3Url)
player.prepareToPlay()
player.play()
} catch let error as NSError {
print(error.description)
}
The sound won't play until I put a breakpoint at player.play() and using po player. Then the sound will be played.
Any idea will be helpful for me.
Try this :
var player: AVAudioPlayer?
func playSound(){
guard let url = Bundle.main.url(forResource: "alert_song", withExtension: "mp3") else {
print("MP3 resource not found.")
return
}
print("Music to play : \(String(describing: url))")
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}
Try this :
var player = AVAudioPlayer() //global variables will otherwise be deadlock
let mp3Url = NSBundle.mainBundle().URLForResource("BCA", withExtension:"mp3")!
do {
player = try AVAudioPlayer(contentsOfURL: mp3Url)
player.play()
} catch let error as NSError {
print(error.description)
}
Currently this is the code I'm using to play my sound during the button press.
var player: AVAudioPlayer?
let url = Bundle.main.url(forResource: "Sound/yatch", withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error {
print(error.localizedDescription)
}
Im trying to get this audio to run over whats currently playing (etc Spotify, pandora, Music). How would I go about doing this?
Set the audio session of your application to play over the currently playing audio:
try? AVAudioSession.sharedInstance().setActive(true)
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
According to the AVAudioSession headers, AVAudioSessionCategoryAmbient will play audio with music, etc.
/* Use this category for background sounds such as rain, car engine noise, etc.
Mixes with other music. */
public let AVAudioSessionCategoryAmbient: String
Swift 5.3 Update
guard let url = Bundle.main.url(forResource: "INSERT_FILENAME", withExtension: "wav") else { return }
do {
try AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.wav.rawValue)
audioPlayer.volume = 1
audioPlayer.play()
} catch let error {
print(error.localizedDescription)
}
Hey guys so for some reason when you mute the phone the audio is still coming from the app. Any ideas on how to fix?
private func myFunc() {
// Set up the audio session
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
fatalError("Unable to set the audio session because: \(error)")
}
guard
let audioURL = Bundle.main.url(forResource: "lololol", withExtension: "mp3"),
let player = try? AVAudioPlayer(contentsOf: audioURL)
else { fatalError("Unable to create player") }
if let aPlayer = audioPlayer {
if audioPlayer?.isPlaying == false {
audioPlayer = player
player.play()
}
} else {
audioPlayer = player
player.play()
}
}
I solve it by changing AVAudioSessionCategoryPlayback to AVAudioSessionCategoryAmbient.
i try to make iOS app to play some funny sound put when i click play no sound are out from speaker here is the code :
#IBAction func slow(sender: UIButton) {
if let filePath = NSBundle.mainBundle().URLForResource("io", withExtension: "mp3") {
let _a = try! AVAudioPlayer(contentsOfURL: filePath)
// Play
_a.play()
}else
{
print("error")
}
}
there is no error but i can't hear any sound at all
for how that face this problem
#IBAction func slow(sender: UIButton) {
if let path = NSBundle.mainBundle().pathForResource("io", ofType:"mp3") {
let fileURL = NSURL(fileURLWithPath: path)
player = try! AVAudioPlayer(contentsOfURL: fileURL)
player.prepareToPlay()
player.play()
}
else
{
print("error")
}
}
}