简体   繁体   English

如何使用soundcloud Javascript SDK播放私人音轨

[英]How play a private track with soundcloud Javascript SDK

I have a problem with my privates tracks. 我的私人跟踪有问题。 I have public track and privates tracks in my soundcloud account. 我的soundcloud帐户中有公共轨道和私有轨道。 When a track is public is correcte, but when i'm a private track the api return an 404 error : GET http://api.soundcloud.com/tracks/123?client_id=xxxxx&format=json&_status_code_map[302]=200 404 (Not Found) in my console. 当一个轨道是公开的是纠正,但当我是私人轨道时,api返回404错误:获取http://api.soundcloud.com/tracks/123?client_id=xxxxx&format=json&_status_code_map[302]=200 404(在我的控制台中找不到)。

Here my function for to play a track : 这是我演奏曲目的功能:

// Play a Single track 
$('#app').on('click','.play_btn_single',function(event){
    event.preventDefault();
    var el = $(this);
    var id = el.attr('id');


    SC.stream('/tracks/'+id,function(sound){
        sound.play();
    });


});

Thank you of your help. 谢谢你的帮助。

You need to provide an authentication token if you wish to play sounds from an account that are not public. 如果您希望播放非公开帐户的声音,则需要提供身份验证令牌。 From SoundCloud's API Guide: 来自SoundCloud的API指南:

"Note that as long as the sound is public, you'll only need to provide a client_id when creating a client. If you would like to access the stream URL for a private sound, you'll need to authenticate your app." “请注意,只要声音是公开的,您只需要在创建客户端时提供client_id。如果您想访问私有声音的流URL,则需要对您的应用进行身份验证。”

For instructions on authentication, see: https://developers.soundcloud.com/docs/api/guide#authentication 有关身份验证的说明,请参阅: https//developers.soundcloud.com/docs/api/guide#authentication

It is possible to play a private track. 可以播放私人赛道。 The easiest is to use private HTML5 widget (as Jameal G. correctly noticed) – for that you just need to click "share" button on a track's page, select "embed" tab and copy the iframe code to insert on your website. 最简单的方法是使用私有HTML5小部件(正如Jameal G.正确注意到的那样) - 为此您只需要点击轨道页面上的“共享”按钮,选择“嵌入”选项卡并复制iframe代码以插入您的网站。

Otherwise, if you want to play it with your own code, then you have to use secret_token that you can get when sharing the track on the website (when authorised) or when querying the API, also authorised (so providing the HTTP header Authorization: Oauth 123… ). 否则,如果你想用你自己的代码来玩,那么你必须使用在网站上共享轨道时(授权时)或查询API时也可以获得的secret_token ,也是授权的(因此提供HTTP头Authorization: Oauth 123… )。

Then the client application can use that secret token to query API and play your sound without authorising the users. 然后,客户端应用程序可以使用该秘密令牌查询API并播放您的声音而无需授权用户。 Below is a simple example where I share my private track: 以下是我分享私人音轨的简单示例:

HTML: HTML:

<audio controls></audio>

JS, assuming jQuery, but the idea should be clear: JS,假设jQuery,但想法应该是明确的:

var clientId = 'client_id=YOUR_CLIENT_ID',
    sekretToken = 'secret_token=s-wwC6c',
    trackUri = 'https://api.soundcloud.com/tracks/128310939.json?' + 
               secretToken + '&' + clientId,
    audio = jQuery('audio');

jQuery
  .get(trackUri)
  .then(function (result) {
    // do not forget to supply client_id also when querying for stream url
    audio.attr('src', result.stream_url + clientId);
  });

http://jsbin.com/yerilu/edit?html,js,output http://jsbin.com/yerilu/edit?html,js,output

UPD . UPD How to find the secret token. 如何找到秘密令牌。

  1. On SoundCloud website, go to the page of the private track that you own, while authorised on the website, then press "share" button (second from the left under "write a comment" input). 在SoundCloud网站上,转到您拥有的私人音乐页面,同时在网站上授权,然后按“分享”按钮(左侧第二个“写评论”输入)。 You will see a text input field titled "Private Share" – copy the link and the last part after the slash is the secret token. 您将看到一个标题为“Private Share”的文本输入字段 - 复制链接,斜杠后面的最后一部分是秘密令牌。

  2. You can also query the API when authorised (so you must send Oauth HTTP header, like Authorization: Oauth 123… , of course you will have a longer number for your own token) for your tracks and the track representation will have secret_token property with the value of the token you could use. 您还可以在授权时查询API(因此您必须发送Oauth HTTP标头,例如Authorization: Oauth 123… ,当然您将拥有更长的自己的令牌编号),并且曲目表示将具有secret_token属性您可以使用的令牌的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM