Skip to main content
5-Regular Member
February 28, 2020
Question

How do you play audio in Hololens?

  • February 28, 2020
  • 1 reply
  • 3233 views

I have a customer that wants to have non-voice sounds in HL2. Something like "ding" or similar. There isn't an audio widget in the options in studio for the 3D canvas.

We have been using the following line of code to synthesize speech but want some non-speech sounds: 

$scope.app.speech.synthesizeSpeech({'text': 'Say this text out loud'});

 

On a related note, can you change the voice that gets used by that line of code?

1 reply

15-Moonstone
March 2, 2020

use this code 

 

$scope.mp3play=function() {

var audioElement;
if(!audioElement) {
audioElement = document.createElement('audio');
audioElement.innerHTML = '<source src="' + 'app/resources/Uploaded/File_Name.mp3'+ '" type="audio/mpeg" />'
}
audioElement.play();
};

 

 

File:Name.mp3 = file audio uploaded into project.

 

 

i hope to help you.

 

@Giuseppe_Fiore 

1-Visitor
April 13, 2020

Hi @Giuseppe_Fiore .

I tried your code. It worked really well. Just one question i have a button which is linked to playing sound. When i press the button sound plays and ends after 10 seconds but when i click the button again before 10 seconds previous sound and the new sound mixes. Can you please tell me a way in which i can allow the first sound to play completely and then only second will be played.

Thank you

21-Topaz I
April 20, 2020

Hi @vivekse ,

you can simple define a listener for the play finish- some thing like this:

//==================================
$scope.app.playAudio = function (mp3) {
var audio = new Audio(mp3);
audio.addEventListener('ended', function() {
 console.warn("mp3="+mp3+" finished!")
 console.warn("call the next audio from the list");
 }, false);
audio.play();
};
// and later use it e.g.
$scope.toggleClick= function()
{
 
 console.log("toggleClick");
 $scope.app.playAudio('app/resources/Uploaded/seq_step_'+GetCurrentStep()+'.mp3');

 For example you can write all mp3 files in an array and play the next :

var mp3arr = ["step1.mp3", "step2.mp3", "step3.mp3", "step.mp3"];
function getNext(mp3arr, mp3) {
var index = mp3arr.indexOf(mp3);
return mp3arr[index+1]
}