How do i add audio to my program? I have a mp3 already in my program










0















I have in my index.html this already, and then i add a function named play where i call in play to my script file[then finally when i try to call in my play command if the player decides to shoot a fireball at the enemy it gives me an error saying "Cannot read property 'play' of null" What do i do?



I have this in my index.html file:



<audio id="audio" src="fire.mp3" ></audio>


At the top of my script file, i do this:



var fire = new Audio();
fire.src = "fire.mp3";
function play()
var audio = document.getElementById("audio");
audio.play();
`


(Fire.mp3 is already a file outside of my program, its what i called in in my index.html file)


Lastly, when the player decides to use fireball, I tried to add the one second fireball noise in by doing this:



if (fire = true && up2 === "FIRE" ) {
var enemyhealth = parseInt(enemyhealth) - parseInt(firepower);
xp + 2;
play();


`
play at the end is when i try to call in the function, but it dosent work.










share|improve this question
























  • Welcome to Stack Overflow! In order to get the best answers, you will need to post code examples. This article explains how to ask a great question: stackoverflow.com/help/how-to-ask

    – Harvey A. Ramer
    Nov 14 '18 at 2:16











  • = does not compare things.

    – SLaks
    Nov 14 '18 at 2:38















0















I have in my index.html this already, and then i add a function named play where i call in play to my script file[then finally when i try to call in my play command if the player decides to shoot a fireball at the enemy it gives me an error saying "Cannot read property 'play' of null" What do i do?



I have this in my index.html file:



<audio id="audio" src="fire.mp3" ></audio>


At the top of my script file, i do this:



var fire = new Audio();
fire.src = "fire.mp3";
function play()
var audio = document.getElementById("audio");
audio.play();
`


(Fire.mp3 is already a file outside of my program, its what i called in in my index.html file)


Lastly, when the player decides to use fireball, I tried to add the one second fireball noise in by doing this:



if (fire = true && up2 === "FIRE" ) {
var enemyhealth = parseInt(enemyhealth) - parseInt(firepower);
xp + 2;
play();


`
play at the end is when i try to call in the function, but it dosent work.










share|improve this question
























  • Welcome to Stack Overflow! In order to get the best answers, you will need to post code examples. This article explains how to ask a great question: stackoverflow.com/help/how-to-ask

    – Harvey A. Ramer
    Nov 14 '18 at 2:16











  • = does not compare things.

    – SLaks
    Nov 14 '18 at 2:38













0












0








0








I have in my index.html this already, and then i add a function named play where i call in play to my script file[then finally when i try to call in my play command if the player decides to shoot a fireball at the enemy it gives me an error saying "Cannot read property 'play' of null" What do i do?



I have this in my index.html file:



<audio id="audio" src="fire.mp3" ></audio>


At the top of my script file, i do this:



var fire = new Audio();
fire.src = "fire.mp3";
function play()
var audio = document.getElementById("audio");
audio.play();
`


(Fire.mp3 is already a file outside of my program, its what i called in in my index.html file)


Lastly, when the player decides to use fireball, I tried to add the one second fireball noise in by doing this:



if (fire = true && up2 === "FIRE" ) {
var enemyhealth = parseInt(enemyhealth) - parseInt(firepower);
xp + 2;
play();


`
play at the end is when i try to call in the function, but it dosent work.










share|improve this question
















I have in my index.html this already, and then i add a function named play where i call in play to my script file[then finally when i try to call in my play command if the player decides to shoot a fireball at the enemy it gives me an error saying "Cannot read property 'play' of null" What do i do?



I have this in my index.html file:



<audio id="audio" src="fire.mp3" ></audio>


At the top of my script file, i do this:



var fire = new Audio();
fire.src = "fire.mp3";
function play()
var audio = document.getElementById("audio");
audio.play();
`


(Fire.mp3 is already a file outside of my program, its what i called in in my index.html file)


Lastly, when the player decides to use fireball, I tried to add the one second fireball noise in by doing this:



if (fire = true && up2 === "FIRE" ) {
var enemyhealth = parseInt(enemyhealth) - parseInt(firepower);
xp + 2;
play();


`
play at the end is when i try to call in the function, but it dosent work.







javascript html audio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 2:21







Drake Griffith

















asked Nov 14 '18 at 2:07









Drake GriffithDrake Griffith

11




11












  • Welcome to Stack Overflow! In order to get the best answers, you will need to post code examples. This article explains how to ask a great question: stackoverflow.com/help/how-to-ask

    – Harvey A. Ramer
    Nov 14 '18 at 2:16











  • = does not compare things.

    – SLaks
    Nov 14 '18 at 2:38

















  • Welcome to Stack Overflow! In order to get the best answers, you will need to post code examples. This article explains how to ask a great question: stackoverflow.com/help/how-to-ask

    – Harvey A. Ramer
    Nov 14 '18 at 2:16











  • = does not compare things.

    – SLaks
    Nov 14 '18 at 2:38
















Welcome to Stack Overflow! In order to get the best answers, you will need to post code examples. This article explains how to ask a great question: stackoverflow.com/help/how-to-ask

– Harvey A. Ramer
Nov 14 '18 at 2:16





Welcome to Stack Overflow! In order to get the best answers, you will need to post code examples. This article explains how to ask a great question: stackoverflow.com/help/how-to-ask

– Harvey A. Ramer
Nov 14 '18 at 2:16













= does not compare things.

– SLaks
Nov 14 '18 at 2:38





= does not compare things.

– SLaks
Nov 14 '18 at 2:38












1 Answer
1






active

oldest

votes


















0














You're using inconsistent methods and objects. Change your code to this:



var fire = document.getElementById("audio");


Or this:



var fire = new Audio("fire.mp3");


And then make sure it's still consistent all the way through your program.






share|improve this answer























  • Hey Jack, so i did that. I deleted all of the code above and in the script at the very top i typed in 'var fireaudio = new Audio("fire.mp3");' then at the time i wanted the audio to play i put 'fireaudio()'. I ran the program, and it came back and said ' script.js:44 Uncaught TypeError: fireaud is not a function'

    – Drake Griffith
    Nov 14 '18 at 2:25











  • You need to play it. Use fireaudio.play() and it will work perfectly.

    – Jack Bashford
    Nov 14 '18 at 2:27










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%2f53292186%2fhow-do-i-add-audio-to-my-program-i-have-a-mp3-already-in-my-program%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You're using inconsistent methods and objects. Change your code to this:



var fire = document.getElementById("audio");


Or this:



var fire = new Audio("fire.mp3");


And then make sure it's still consistent all the way through your program.






share|improve this answer























  • Hey Jack, so i did that. I deleted all of the code above and in the script at the very top i typed in 'var fireaudio = new Audio("fire.mp3");' then at the time i wanted the audio to play i put 'fireaudio()'. I ran the program, and it came back and said ' script.js:44 Uncaught TypeError: fireaud is not a function'

    – Drake Griffith
    Nov 14 '18 at 2:25











  • You need to play it. Use fireaudio.play() and it will work perfectly.

    – Jack Bashford
    Nov 14 '18 at 2:27















0














You're using inconsistent methods and objects. Change your code to this:



var fire = document.getElementById("audio");


Or this:



var fire = new Audio("fire.mp3");


And then make sure it's still consistent all the way through your program.






share|improve this answer























  • Hey Jack, so i did that. I deleted all of the code above and in the script at the very top i typed in 'var fireaudio = new Audio("fire.mp3");' then at the time i wanted the audio to play i put 'fireaudio()'. I ran the program, and it came back and said ' script.js:44 Uncaught TypeError: fireaud is not a function'

    – Drake Griffith
    Nov 14 '18 at 2:25











  • You need to play it. Use fireaudio.play() and it will work perfectly.

    – Jack Bashford
    Nov 14 '18 at 2:27













0












0








0







You're using inconsistent methods and objects. Change your code to this:



var fire = document.getElementById("audio");


Or this:



var fire = new Audio("fire.mp3");


And then make sure it's still consistent all the way through your program.






share|improve this answer













You're using inconsistent methods and objects. Change your code to this:



var fire = document.getElementById("audio");


Or this:



var fire = new Audio("fire.mp3");


And then make sure it's still consistent all the way through your program.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 2:16









Jack BashfordJack Bashford

9,94731541




9,94731541












  • Hey Jack, so i did that. I deleted all of the code above and in the script at the very top i typed in 'var fireaudio = new Audio("fire.mp3");' then at the time i wanted the audio to play i put 'fireaudio()'. I ran the program, and it came back and said ' script.js:44 Uncaught TypeError: fireaud is not a function'

    – Drake Griffith
    Nov 14 '18 at 2:25











  • You need to play it. Use fireaudio.play() and it will work perfectly.

    – Jack Bashford
    Nov 14 '18 at 2:27

















  • Hey Jack, so i did that. I deleted all of the code above and in the script at the very top i typed in 'var fireaudio = new Audio("fire.mp3");' then at the time i wanted the audio to play i put 'fireaudio()'. I ran the program, and it came back and said ' script.js:44 Uncaught TypeError: fireaud is not a function'

    – Drake Griffith
    Nov 14 '18 at 2:25











  • You need to play it. Use fireaudio.play() and it will work perfectly.

    – Jack Bashford
    Nov 14 '18 at 2:27
















Hey Jack, so i did that. I deleted all of the code above and in the script at the very top i typed in 'var fireaudio = new Audio("fire.mp3");' then at the time i wanted the audio to play i put 'fireaudio()'. I ran the program, and it came back and said ' script.js:44 Uncaught TypeError: fireaud is not a function'

– Drake Griffith
Nov 14 '18 at 2:25





Hey Jack, so i did that. I deleted all of the code above and in the script at the very top i typed in 'var fireaudio = new Audio("fire.mp3");' then at the time i wanted the audio to play i put 'fireaudio()'. I ran the program, and it came back and said ' script.js:44 Uncaught TypeError: fireaud is not a function'

– Drake Griffith
Nov 14 '18 at 2:25













You need to play it. Use fireaudio.play() and it will work perfectly.

– Jack Bashford
Nov 14 '18 at 2:27





You need to play it. Use fireaudio.play() and it will work perfectly.

– Jack Bashford
Nov 14 '18 at 2:27



















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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292186%2fhow-do-i-add-audio-to-my-program-i-have-a-mp3-already-in-my-program%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

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20