SoundManager的Soundcloud API不会返回peakData

时间:2015-05-27 16:19:08

标签: javascript api soundcloud soundmanager2

我正在使用SoundManager2从SoundCloud流式传输音乐。但是,它并没有向我返回peakData(或WaveformData或EQData)。

我尝试过直接使用SoundManager2和SoundCloud API Wrapper,结果相同。

我想要peakData数据以创建可视化。我不能使用普通的HTML5 Audio API播放器,因为我需要SoundManager(或Soundcloud API包装器)返回的其他函数。

播放曲目,但soundManager.features返回peakData为false且峰值数据返回&0 39'

的数组。

代码如下,这里有一个简化设置的私有CodePen: SoundCloud Test

SC.initialize({
    client_id: 'b5ec05bfa8844fff9b84362925f46745'
});

var soundcloud_track;
SC.get('/tracks/202764956', {}, function(track){
    soundcloud_track = track.stream_url+'?client_id=b5ec05bfa8844fff9b84362925f46745';

    streamTrack();
});

function streamTrack(){
    soundManager.setup = {
        preferFlash: true,
    }

    soundManager.flash9Options = {
        usePeakData: true,     // enable left/right channel peak (level) data
        useWaveformData: true, // enable sound spectrum (raw waveform data) - WARNING: May set CPUs on fire.
        useEQData: true,       // enable sound EQ (frequency spectrum data) - WARNING: Also CPU-intensive.
    }

    var sound = soundManager.createSound({
        url: soundcloud_track,
        autoPlay: true,
        onplay: function(){
            console.log(soundManager.features);
        },
        whileplaying: function(){
            console.log(sound.peakData);
        }
    });
}

3 个答案:

答案 0 :(得分:0)

如果您只想要数据:

Soundcloud开始直接提供peakdata。如果你仍然得到PNG:

获得它:

  • 检索波形PNG
  • 将扩展名更改为.json
  • 将w1更改为wis

在你的情况下:

https://w1.sndcdn.com/Oz5FJMJhr8Ho_m.png 成为 https://wis.sndcdn.com/Oz5FJMJhr8Ho_m.json

你得到它!

如果您还想绘制它:

http://waveformjs.org/

答案 1 :(得分:0)

似乎有一个bug或者什么,我也有0个值。

这可能是因为这些参数需要 flash9 +

zero

如何使用最后一个答案,以便获得完整的波形数据,并且您只需获取您在特定时间所需的数据?

比每次需要峰值数据时拨打电话更好;它完全兼容。

答案 2 :(得分:0)

如果有人想从图像文件中获取峰值数据并准备在服务器端执行该操作,这可能会有所帮助。

前段时间我创建了一个PHP脚本,用波形数据检索PNG图像,扫描峰值并将其作为JSON对象返回。

我将按原样提供我的代码,您需要mLoad函数中的代码,且峰值位于$tmpPeaks

$tmpReturnO只是我用来返回数据和/或错误和东西的对象,忽略它。)

您可以看到peakdata waveform code in action here

该文件未被触及,因此您必须提取所需内容,这应该是自我解释的,有关详细信息,请参阅complete thread on github

<?php

/**
 * waveformClass 
 * retrieves the waveform image from soundcloud
 * Analyses it for peakdata and returns those peaks in an array or binary string
 */

class waveformClass extends baseClass {

    private $pParentO;      

    public $pURL = '';
    public $pPeakData = '';

    public function __construct(moireClass &$parentOIn) {
        parent::mInit();
        $this->pParentO = $parentOIn;
    }

   /** 
    * mLoad, load the wavefile from remote URL
    * 
    * @param $URLIn string URI of the png image
    * @param $doBinary boolean when true, instead of an array, 
    * a binary bytestring consisting of 1800 bytes will be returned 
    * For demonstration purpose - to show how to shave off datatraffic
    * on peakdata array loading vs loading of a png image
    * 
    * @return result an array with the peak-heights or a binary string
    */

    public function mLoad($URLIn, $doBinary = false) {

        $tmpBinaryString = "";
        $tmpReturnO = $this->_mGetReturnObject();

        $width = 1800;
        $height = 280;

        $srcimage = imagecreatefrompng($URLIn);

        imageAlphaBlending($srcimage, true);
        imageSaveAlpha($srcimage, true);

        $tmpPeaks =  array(); 

        for ($x=0;  $x<1800; $x++) { // the width of the image is 1800
            for ($y=0; $y<140; $y++) { // the image is symmetrical, so we only take (140 pixels) the half of the height (of 280 pixels)

                $rgb = imagecolorat($srcimage, $x, $y);

                $colors = imagecolorsforindex($srcimage, $rgb);

                if ($colors['alpha'] == 127) {
                    break;
                }               
            }
            $tmpPeaks[] = 140-$y;
        }


        if ($doBinary) {
            $tmpBinaryString = call_user_func_array("pack", array_merge(array("C*"), $tmpPeaks));

            $tmpReturnO->mOk($tmpBinaryString);
        } else {
            $tmpReturnO->mOk($tmpPeaks);
        }

        return $tmpReturnO->mGetReturnRecord();

    }
}
?>