2016-09-30 4 views
1

я использовал JS код аудио API для визуализации мой html5 аудио и он работал большой здесь учебник/кодAnalyzer Bars Анимация HTML Audio API не работает

ссылка: https://www.developphp.com/video/Jav...o-API-Tutorial

и работает Су хорошо но проблема, когда я изменить СРК с внешней ссылкой, как:

SRC = «http://example.com/file.mp3»

звук не делает звук и визуализация не показывает, хотя, когда я удалить код визуализации это работает норму союзник ... я попытался изменить

context.createMediaElementSource(audio); 

с

AudioContext.createMediaStreamDestination() 

звук играл, но не визуализация до сих пор .. я не знаю, что я могу делать какие-либо идеи?

<!doctype html> 
 
<html> 
 
<head> 
 
<meta charset="utf-8"/> 
 
<meta http-equiv="Access-Control-Allow-Origin" content="*"> 
 
<style> 
 
div#mp3_player{ width:500px; height:60px; background:#000; padding:5px; margin:50px auto; } 
 
div#mp3_player > div > audio{ width:500px; background:#000; float:left; } 
 
div#mp3_player > canvas{ width:500px; height:30px; background:#002D3C; float:left; } 
 
</style> 
 
<script> 
 
// Create a new instance of an audio object and adjust some of its properties 
 
var audio = new Audio(); 
 
// audio.crossOrigin='anonymous'; 
 
audio.src = 'http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3'; 
 
audio.controls = true; 
 
audio.loop = true; 
 
audio.autoplay = true; 
 
// Establish all variables that your Analyser will use 
 
var canvas, ctx, source, context, analyser, fbc_array, bars, bar_x, bar_width, bar_height; 
 
// Initialize the MP3 player after the page loads all of its HTML into the window 
 
window.addEventListener("load", initMp3Player, false); 
 
function initMp3Player(){ 
 
\t document.getElementById('audio_box').appendChild(audio); 
 
\t context = new AudioContext(); // AudioContext object instance 
 
\t analyser = context.createAnalyser(); // AnalyserNode method 
 
\t canvas = document.getElementById('analyser_render'); 
 
\t ctx = canvas.getContext('2d'); 
 
\t // Re-route audio playback into the processing graph of the AudioContext 
 
\t 
 
\t source = context.createMediaStreamDestination(audio); \t 
 
\t 
 
\t source.connect(analyser); 
 
\t analyser.connect(context.destination); 
 
\t frameLooper(); 
 

 
// frameLooper() animates any style of graphics you wish to the audio frequency 
 
// Looping at the default frame rate that the browser provides(approx. 60 FPS) 
 
function frameLooper(){ 
 
\t window.requestAnimationFrame(frameLooper); 
 
\t fbc_array = new Uint8Array(analyser.frequencyBinCount); 
 
\t analyser.getByteFrequencyData(fbc_array); 
 
\t ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas 
 
\t ctx.fillStyle = '#00CCFF'; // Color of the bars 
 
\t bars = 100; 
 
\t for (var i = 0; i < bars; i++) { 
 
\t \t bar_x = i * 3; 
 
\t \t bar_width = 2; 
 
\t \t bar_height = -(fbc_array[i]/2); 
 
\t \t // fillRect(x, y, width, height) // Explanation of the parameters below 
 
\t \t ctx.fillRect(bar_x, canvas.height, bar_width, bar_height); 
 
\t } 
 
} 
 
} 
 

 
</script> 
 
</head> 
 
<body> 
 
<div id="mp3_player"> 
 
    <div id="audio_box"></div> 
 
    <canvas id="analyser_render"></canvas> 
 
</div> 
 
</body> 
 
</html>

ответ

0

Проблема, вероятно, лежит в узле анализатора. Он может обрабатывать только носители, загруженные в соответствии с CORS.

Если сервер, обслуживающий ваш носитель, составляет well configured, вы можете попробовать установить атрибут аудио элемента, crossOrigin, в противном случае вам придется обслуживать файл из своего собственного домена.

+0

@Sabreena Это делается на стороне сервера, прочитайте ссылку, которую я дал вам в своем ответе. – Kaiido

+0

Спасибо за ответ, получил решение :-) – Sabreena

+0

@ Сабрина, рад, что это помогло; Если ответ помог вам исправить вашу проблему, вы более чем можете [принять это] (http://stackoverflow.com/help/someone-answers). Таким образом, он отключится от потока * для ответа *. – Kaiido

Смежные вопросы