5

我正在尝试使用 soundcloud API 和 javascript 将歌曲上传到 soundcloud。但我似乎无法做到。这是我的 javascript

function ADDTRACK() {
    var test = document.getElementById('SongTrack').files[0];
    SC.get("http://api.soundcloud.com/users/redstr/tracks.json?client_id=3200814596a029b47877e63edfe6066c", {
        limit: 1
    }, function(tracks) {
        SC.POST({
            tracks: {
                description: 'This track was recorded in Berlin',
                asset_data: '@' + test
            }
        });
    });
}

这是我的 HTML 上传

  <input type="file" id ="SongTrack" name="pic" accept="audio/*" />
      <button type="button"   onclick="ADDTRACK()" />Add TRACK</button>

我没有错误,所以有人能指出我做错了什么吗?

4

2 回答 2

2

来自:http: //developers.soundcloud.com/docs/api/guide#uploading

// the JavaScript SDK does not by itself have access
// to the filesystem, so instead this example records
// some audio in the browser and uploads it.

        SC.connect(function() {
          SC.record({
            start: function() {
              window.setTimeout(function() {
                SC.recordStop();
                SC.recordUpload({
                  track: { title: 'This is my sound' }
                });
              }, 5000);
            }
          }    
    });

所以你不能直接用javascript上传曲目。

于 2012-10-31T13:19:35.953 回答
1

我最终也想通过 Javascript SDK 上传音频文件,所以我对 PHP 和 JS SDK 进行了逆向工程以使其工作。

首先,您应该确保 Javascript SDK 正常工作。我们仅将它用于身份验证令牌(使用未记录的方法)。除了令牌之外,其余部分都是使用对象手动处理$.ajaxFormData

HTML:

<script src="//cdn.jsdelivr.net/jquery/1.8.3/jquery-1.8.3.min.js"></script>
<script src="//connect.soundcloud.com/sdk.js"></script>
<button id="login">log in</button>
<form>
    <label>Title: <input name="title" disabled /></label><br>
    <label>File: <input name="file" type="file" accept="audio/*" disabled /></label><br>
    <button type="submit" disabled >Submit</button>
</form>

Javascript:

SC.initialize({
    client_id: "INSERT_CLIENT_ID",
    redirect_uri: "INSERT_REDIRECT_URI"
});

$("#login").click(function(){
    SC.connect(function(){
        $('form input,form button').removeAttr('disabled');
    });
});

$('form').submit(function(e) {
    var fd = new FormData();
    fd.append('oauth_token', SC.accessToken());
    fd.append('format','json');
    fd.append("track[title]", $(this).find('input[name=title]').val());
    fd.append("track[asset_data]", $(this).find('input[name=file]').prop('files')[0]);

    $.ajax({
        url: 'https://api.soundcloud.com/v1/tracks',
        type: 'POST',
        data: fd,
        processData: false,
        contentType: false,
        xhr: function() {
            var xhr = $.ajaxSettings.xhr();
            xhr.upload.onprogress = function(e) {
                if(e.lengthComputable) {
                    var percent = Math.floor((e.loaded / e.total) * 100);
                    console.log(percent  + '% uploaded');
                }
            };
            return xhr;
        }
    }).done(function(e) {
        console.log('Upload Complete!');
        console.dir(e); // This is the JSON object of the resulting track
    });
    e.preventDefault();
});
于 2015-05-21T20:56:33.257 回答