AVAudioPlayerNode does not play sound












0














The AudioPlayerNode is an instance variable code is a follows:



class HXAudioEngine {
private var audioEngine: AVAudioEngine = AVAudioEngine()

var digitFileUrl: URL? {
didSet {
if let digitUrl = digitFileUrl {
do {
digitAudioFile = try AVAudioFile(forReading: digitUrl)
} catch let error {
print("Error loading Digit file: (error.localizedDescription)")
}
}
}
}
var digitAudioFile: AVAudioFile? {
didSet {
if let digitFile = digitAudioFile {
digitAudioFormat = digitFile.processingFormat
digitFileBuffer = AVAudioPCMBuffer(pcmFormat: digitFile.processingFormat, frameCapacity: UInt32(digitFile.length))
}
}
}
var digitFileBuffer: AVAudioPCMBuffer? {
didSet {
if let buffer = digitFileBuffer {
do {
try digitAudioFile?.read(into: buffer)
} catch let error {
print("Error loading digit file into buffer: (error.localizedDescription)")
}
}
}
}
var digitAudioFormat: AVAudioFormat?
var digitPlayer: AVAudioPlayerNode = AVAudioPlayerNode()

func playDigit() {
let file = "d0p1m00db"
digitFileUrl = Bundle.main.url(forResource: file, withExtension: "wav")
audioEngine.attach(digitPlayer)
audioEngine.connect(digitPlayer, to: audioEngine.mainMixerNode, format: digitAudioFormat)
audioEngine.prepare()

do {
try audioEngine.start()
} catch let error {
print(error)
}

guard let digitBuffer = digitFileBuffer else { return }
guard let digitAudioFile = digitAudioFile else { return }

digitPlayer.scheduleBuffer(digitBuffer, at: nil, options: .interrupts) {
print("Done playing digit")
}
digitPlayer.play()
}
}


For some reason the file that ends up being played is just a click sound. The audio file is a 1 second long file of a lady speaking one digit.



It almost seems like the player speeds through the sound file and hence obscures the speech. The sample rate is 44100. Reading on Stack Overflow there is alot of solutions talking about the audioplayer being deallocated before the sound file is completed.



But I have tested on a separate app built through Raywenderlich tutorial: https://www.raywenderlich.com/5154-avaudioengine-tutorial-for-ios-getting-started, which actually shows the length of the sound file and the playing progress. And all seems fine but even then the audio file doesn't play properly.



Is there an issue with my implementation or is it the audio file? The audio file plays fine on itunes.










share|improve this question






















  • So interestingly the audio file doesn't play properly if the output is the Device speaker but will play normally if the headphones are plugged in. Does anyone know why this would be the case?
    – Alexander
    2 days ago
















0














The AudioPlayerNode is an instance variable code is a follows:



class HXAudioEngine {
private var audioEngine: AVAudioEngine = AVAudioEngine()

var digitFileUrl: URL? {
didSet {
if let digitUrl = digitFileUrl {
do {
digitAudioFile = try AVAudioFile(forReading: digitUrl)
} catch let error {
print("Error loading Digit file: (error.localizedDescription)")
}
}
}
}
var digitAudioFile: AVAudioFile? {
didSet {
if let digitFile = digitAudioFile {
digitAudioFormat = digitFile.processingFormat
digitFileBuffer = AVAudioPCMBuffer(pcmFormat: digitFile.processingFormat, frameCapacity: UInt32(digitFile.length))
}
}
}
var digitFileBuffer: AVAudioPCMBuffer? {
didSet {
if let buffer = digitFileBuffer {
do {
try digitAudioFile?.read(into: buffer)
} catch let error {
print("Error loading digit file into buffer: (error.localizedDescription)")
}
}
}
}
var digitAudioFormat: AVAudioFormat?
var digitPlayer: AVAudioPlayerNode = AVAudioPlayerNode()

func playDigit() {
let file = "d0p1m00db"
digitFileUrl = Bundle.main.url(forResource: file, withExtension: "wav")
audioEngine.attach(digitPlayer)
audioEngine.connect(digitPlayer, to: audioEngine.mainMixerNode, format: digitAudioFormat)
audioEngine.prepare()

do {
try audioEngine.start()
} catch let error {
print(error)
}

guard let digitBuffer = digitFileBuffer else { return }
guard let digitAudioFile = digitAudioFile else { return }

digitPlayer.scheduleBuffer(digitBuffer, at: nil, options: .interrupts) {
print("Done playing digit")
}
digitPlayer.play()
}
}


For some reason the file that ends up being played is just a click sound. The audio file is a 1 second long file of a lady speaking one digit.



It almost seems like the player speeds through the sound file and hence obscures the speech. The sample rate is 44100. Reading on Stack Overflow there is alot of solutions talking about the audioplayer being deallocated before the sound file is completed.



But I have tested on a separate app built through Raywenderlich tutorial: https://www.raywenderlich.com/5154-avaudioengine-tutorial-for-ios-getting-started, which actually shows the length of the sound file and the playing progress. And all seems fine but even then the audio file doesn't play properly.



Is there an issue with my implementation or is it the audio file? The audio file plays fine on itunes.










share|improve this question






















  • So interestingly the audio file doesn't play properly if the output is the Device speaker but will play normally if the headphones are plugged in. Does anyone know why this would be the case?
    – Alexander
    2 days ago














0












0








0







The AudioPlayerNode is an instance variable code is a follows:



class HXAudioEngine {
private var audioEngine: AVAudioEngine = AVAudioEngine()

var digitFileUrl: URL? {
didSet {
if let digitUrl = digitFileUrl {
do {
digitAudioFile = try AVAudioFile(forReading: digitUrl)
} catch let error {
print("Error loading Digit file: (error.localizedDescription)")
}
}
}
}
var digitAudioFile: AVAudioFile? {
didSet {
if let digitFile = digitAudioFile {
digitAudioFormat = digitFile.processingFormat
digitFileBuffer = AVAudioPCMBuffer(pcmFormat: digitFile.processingFormat, frameCapacity: UInt32(digitFile.length))
}
}
}
var digitFileBuffer: AVAudioPCMBuffer? {
didSet {
if let buffer = digitFileBuffer {
do {
try digitAudioFile?.read(into: buffer)
} catch let error {
print("Error loading digit file into buffer: (error.localizedDescription)")
}
}
}
}
var digitAudioFormat: AVAudioFormat?
var digitPlayer: AVAudioPlayerNode = AVAudioPlayerNode()

func playDigit() {
let file = "d0p1m00db"
digitFileUrl = Bundle.main.url(forResource: file, withExtension: "wav")
audioEngine.attach(digitPlayer)
audioEngine.connect(digitPlayer, to: audioEngine.mainMixerNode, format: digitAudioFormat)
audioEngine.prepare()

do {
try audioEngine.start()
} catch let error {
print(error)
}

guard let digitBuffer = digitFileBuffer else { return }
guard let digitAudioFile = digitAudioFile else { return }

digitPlayer.scheduleBuffer(digitBuffer, at: nil, options: .interrupts) {
print("Done playing digit")
}
digitPlayer.play()
}
}


For some reason the file that ends up being played is just a click sound. The audio file is a 1 second long file of a lady speaking one digit.



It almost seems like the player speeds through the sound file and hence obscures the speech. The sample rate is 44100. Reading on Stack Overflow there is alot of solutions talking about the audioplayer being deallocated before the sound file is completed.



But I have tested on a separate app built through Raywenderlich tutorial: https://www.raywenderlich.com/5154-avaudioengine-tutorial-for-ios-getting-started, which actually shows the length of the sound file and the playing progress. And all seems fine but even then the audio file doesn't play properly.



Is there an issue with my implementation or is it the audio file? The audio file plays fine on itunes.










share|improve this question













The AudioPlayerNode is an instance variable code is a follows:



class HXAudioEngine {
private var audioEngine: AVAudioEngine = AVAudioEngine()

var digitFileUrl: URL? {
didSet {
if let digitUrl = digitFileUrl {
do {
digitAudioFile = try AVAudioFile(forReading: digitUrl)
} catch let error {
print("Error loading Digit file: (error.localizedDescription)")
}
}
}
}
var digitAudioFile: AVAudioFile? {
didSet {
if let digitFile = digitAudioFile {
digitAudioFormat = digitFile.processingFormat
digitFileBuffer = AVAudioPCMBuffer(pcmFormat: digitFile.processingFormat, frameCapacity: UInt32(digitFile.length))
}
}
}
var digitFileBuffer: AVAudioPCMBuffer? {
didSet {
if let buffer = digitFileBuffer {
do {
try digitAudioFile?.read(into: buffer)
} catch let error {
print("Error loading digit file into buffer: (error.localizedDescription)")
}
}
}
}
var digitAudioFormat: AVAudioFormat?
var digitPlayer: AVAudioPlayerNode = AVAudioPlayerNode()

func playDigit() {
let file = "d0p1m00db"
digitFileUrl = Bundle.main.url(forResource: file, withExtension: "wav")
audioEngine.attach(digitPlayer)
audioEngine.connect(digitPlayer, to: audioEngine.mainMixerNode, format: digitAudioFormat)
audioEngine.prepare()

do {
try audioEngine.start()
} catch let error {
print(error)
}

guard let digitBuffer = digitFileBuffer else { return }
guard let digitAudioFile = digitAudioFile else { return }

digitPlayer.scheduleBuffer(digitBuffer, at: nil, options: .interrupts) {
print("Done playing digit")
}
digitPlayer.play()
}
}


For some reason the file that ends up being played is just a click sound. The audio file is a 1 second long file of a lady speaking one digit.



It almost seems like the player speeds through the sound file and hence obscures the speech. The sample rate is 44100. Reading on Stack Overflow there is alot of solutions talking about the audioplayer being deallocated before the sound file is completed.



But I have tested on a separate app built through Raywenderlich tutorial: https://www.raywenderlich.com/5154-avaudioengine-tutorial-for-ios-getting-started, which actually shows the length of the sound file and the playing progress. And all seems fine but even then the audio file doesn't play properly.



Is there an issue with my implementation or is it the audio file? The audio file plays fine on itunes.







swift audio avaudioengine avaudioplayernode






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 27 at 13:12









Alexander

13619




13619












  • So interestingly the audio file doesn't play properly if the output is the Device speaker but will play normally if the headphones are plugged in. Does anyone know why this would be the case?
    – Alexander
    2 days ago


















  • So interestingly the audio file doesn't play properly if the output is the Device speaker but will play normally if the headphones are plugged in. Does anyone know why this would be the case?
    – Alexander
    2 days ago
















So interestingly the audio file doesn't play properly if the output is the Device speaker but will play normally if the headphones are plugged in. Does anyone know why this would be the case?
– Alexander
2 days ago




So interestingly the audio file doesn't play properly if the output is the Device speaker but will play normally if the headphones are plugged in. Does anyone know why this would be the case?
– Alexander
2 days ago

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945686%2favaudioplayernode-does-not-play-sound%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945686%2favaudioplayernode-does-not-play-sound%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas