【问题标题】:Soundcloud API with SoundManager doesn't return peakData带有 SoundManager 的 Soundcloud API 不返回 peakData
【发布时间】:2015-05-27 16:19:07
【问题描述】:

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

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

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

曲目播放,但 soundManager.features 返回 peakData 为 false,并且返回峰值数据和数组“0”。

代码如下,这里有一个简化设置的私有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);
        }
    });
}

【问题讨论】:

  • 告诉我这个答案是否是你要找的

标签: javascript api soundcloud soundmanager2


【解决方案1】:

如果您只想要数据:

Soundcloud 开始直接给出 peakdata。如果您仍然获得 PNG:

得到它:

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

在你的情况下:

https://w1.sndcdn.com/Oz5FJMJhr8Ho_m.png 变得 https://wis.sndcdn.com/Oz5FJMJhr8Ho_m.json

你懂的!


如果你也想画它:

http://waveformjs.org/

【讨论】:

  • 谢谢,不幸的是,我正在寻找由 SoundManager2 返回的实时值 peakData,而不是 Soundcloud 中的波形本身。但感谢您的帮助。
【解决方案2】:

好像有bug什么的,我的值也是0。

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

使用最后一个答案怎么样,这样您就可以获得完整的波形数据,而您只需在特定时间获取您需要的数据?

这比每次需要峰值数据时都打电话要好;并且完全兼容。

【讨论】:

  • 谢谢。可能会到那个地步。但理想情况下,我希望对声音进行比波形产生的更多的分析。不过,我会记住它作为最后的手段:)
【解决方案3】:

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

不久前,我创建了一个 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();

    }
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多