javascript - soundcloud api和web音频api不能一起使用

标签 javascript audio soundcloud web-audio-api

今天,我添加了一种允许用户在网站上使用soundcloud音乐的方法,除了它仅在我删除由网络音频api生成的分析器时才有效。

这是将soundcloud链接附加到音频标签的代码:

$(document).keydown(function(e){
    if (e.which == 13 && $("#customSong").is(":focus")){
        var customSongLink = $("#customSong").val();

        SC.get('/resolve', { url: customSongLink }, function(sound) {
            SC.get("/tracks/" + sound.id, {}, function(sound){
                $("#Musique").attr("src", sound.uri+"/stream?client_id=MY_CLIENT_ID" );

                $(".mediaName").html("<span></span>");
                $(".mediaName span").html(sound.user.username+" - "+sound.title);
                $(".mediaName").textfill();
            });
        });
    }
});

这是可视化工具的代码:
var canvas, ctx, source, context, analyser, fbc_array, bar_x, bar_height;
function initVisualizer() {
    context = new AudioContext();
    analyser = context.createAnalyser();
    biquad = context.createBiquadFilter();
    gainNode = context.createGain();
    canvas = document.getElementById("visualizer");
    ctx = canvas.getContext('2d');
    ctx.fillStyle = "#3f3f3f";


    analyser.smoothingTimeConstant = 0.8;
    biquad.frequency.value = 15000;
    gainNode.gain.value = 1;

    source = context.createMediaElementSource(Musique);
    source.connect(biquad);
    biquad.connect(gainNode);
    gainNode.connect(analyser);
    analyser.connect(context.destination);

    $("#frequencyNumber").html(biquad.frequency.value);
    $("#visualizerSensibilityNumber").html(analyser.smoothingTimeConstant);
    $("#gainNumber").html(gainNode.gain.value.toPrecision(3));

    framelooper()
}

function framelooper() {
    window.requestAnimationFrame(framelooper);
    fbcArray = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(fbcArray);
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (i=0; i < bars; i++) {
        bar_x = i * bar_x_spaces + 0.5;
        bar_height = -(fbcArray[i] / bar_height_sensibility);

        if (visualizerStyle == 1){
            //Simple
            ctx.fillRect(bar_x, canvas.height, bar_width, bar_height);
            $("#visualizerStyleType").html("Simple");
        }
        else if (visualizerStyle == 2) {
            //Reflection
            ctx.fillRect(bar_x, canvas.height/2, bar_width, bar_height/2);
            ctx.fillRect(bar_x, canvas.height/2, bar_width, -bar_height/2);
            $("#visualizerStyleType").html("Reflection");
        }
        else {
            //Two-faced
            ctx.fillRect(0, bar_x, -bar_height, bar_width);
            ctx.fillRect(canvas.width, bar_x, bar_height, bar_width);
            $("#visualizerStyleType").html("Face to Face");
        }
    }
}

website with the issue (double click on the song title to make the input box appear, enter a soundcloud link inside)

编辑:我发现问题是由于音频文件的跨域更改而引起的,我还看到很多人说添加“crossOrigin =匿名”可以解决此问题,但这对我来说不是。

会仅对某些音乐执行此操作,还是不再可能解决此问题?

(如果稍后),还有其他解决方法吗?

最佳答案

仅仅设置crossOrigin是不够的。服务器还必须通过返回适当的 header 来允许跨域访问。这可能不在您的控制范围内。

关于javascript - soundcloud api和web音频api不能一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30267239/

相关文章:

javascript - 在缓存页面上触发 Javascript?

javascript - 如何合并两个 channel 的原始pcm数据?

python - 如何使用 python 3 将专辑封面添加到 mp3 文件?

json - SoundCloud,按查询过滤特定用户的轨道不起作用

ios - SoundCloud API 是否在没有通知的情况下发生了变化?

javascript - 删除被单击元素的父元素

javascript - 每次新点击都有新 Action

javascript - 从 Chrome 应用中的剪贴板获取当前文本

android - 使用Titanium AudioPlayer使用RTSP媒体服务器播放音频(mp3文件)流

jquery - 如何通过 SoundCloud API 一次只播放一首歌曲