Movatterモバイル変換


[0]ホーム

URL:


LoginSignup
3

Go to list of users who liked

1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

経緯はこちら。

きっかけはこちら。

成果物はこちら。

A-Frameで音楽を聴く

公式のドキュメントを確認します。

画像ファイル(再生・停止ボタン)と任意の音楽ファイル(mp3)を用意します。

ソースコード

追加するA-Frame(a-scene)のパーツです。

index.htm
<a-cameraposition="0 -320 0"cursor="rayOrigin: mouse;"></a-camera><a-assets><imgid="img_play"src="img_play.png"/></a-assets><a-assets><imgid="img_pause"src="img_pause.png"/></a-assets><a-assets><audioid="audio01"preload="auto"src="音楽.mp3"/></a-assets><a-entitysound="src: #audio01; autoplay: false; positional: false; loop: true"></a-entity><a-imageid="button01"src="#img_pause"position="0 -300 -200"scale="20 20 20"/>

イベント(クリック)をトリガーとして音楽を再生(停止)、ボタンを表示(変更)します。

main.js
"use strict";// Windowwindow.onload=(e)=>{console.log("onload!!");constscene=document.querySelector("a-scene");constentity=document.querySelector('[sound]');constbutton01=document.getElementById("button01");button01.addEventListener("click",(e)=>{console.log("clicked!!");console.log(entity);if(entity.components.sound.isPlaying){console.log("paused!");entity.components.sound.pauseSound();e.target.setAttribute("src","#img_pause");}else{console.log("played!");entity.components.sound.playSound();e.target.setAttribute("src","#img_play");}});}

NT金沢2025で体験(出展)

今週末(6/21(土)〜22(日))のNT金沢2025(うH)でお待ちしております!

3

Go to list of users who liked

1
0

Go to list of comments

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3

Go to list of users who liked

1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?


[8]ページ先頭

©2009-2025 Movatter.jp