创建PositionalAudio并播放音频
方式一:直接创建对象
var listener = new THREE.AudioListener();
var audio_p;
camera.add( listener );
audioLoader.load( 'download/avatar/?fileName=016377.wav',
function ( buffer ) {
var audio = new THREE.PositionalAudio( listener );
audio.setBuffer( buffer );
audio.setRefDistance( 40 )
if(object instanceof THREE.Object3D) object.add( audio );
//object为加载的模型内容
scene.add( body );
audio_p=cue.children[ 0 ]
// animate();
} );
方式二:界面有对应的audio元素
### html
<audio loop id="music" preload="auto" style="display: none">
<source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg" type="audio/ogg">
<source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3" type="audio/mpeg">
</audio>
### js
var listener = new THREE.AudioListener();
camera.add( listener );
var audioElement = document.getElementById( 'music' );
var audio_p = new THREE.PositionalAudio( listener );
audio_p.setMediaElementSource( audioElement );
audio_p.setRefDistance( 1 );
audio_p.setDirectionalCone( 210, 230, 0.1 );
创建后进行播放等操作
audio_p为一个PositionalAudio对象;
1. **播放和暂停:** audio_p.play();audio_p.stop();
2. **获取当前播放时间:** audio_p.context.currentTime-audio_p.startTime;
3. **判断是否正在播放:** audio_p.isPlaying;