Help us understand the problem. What is going on with this article?

javascriptでmp3ファイルを再生

HTMLを編集

<button id="btn">再生</button>
<button id="btn2">停止</button>

<audio id="audio"></audio>

これらを追加する

javascriptを書く

HTMLの中にscriptタグをかいてHTMLにjavascriptを書くか、
javascriptファイルを作ってそれでjavascriptを書くか。
今回はめんどくさいのでHTMLのなかにjavascriptを書きます。 headタグ内に書いてみてください
。 エラーでたら、bodyタグ内に書いてみましょう。

<script>
var btn = document.getElementById("btn"); //document.getElementByIdを使ってHTMLのbtnというIDを取得
var btn2 = document.getElementById("btn2");//document.getElementByIdを使ってHTMLのbtn2というIDを取得

btn.addEventListener('click', function(){   //もしbtn(再生)をクリックしたら..
   audio.src='ここにmp3ファイル名を書く';
   audio.play(); //audioを再生
});

btn.addEventListener('click', function(){
   audio.pause(); //audioを止める
   audio.currentTime = 0; //時間を0に
});
</script>

これで再生と停止ができたみたいです!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした