最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get correct .mp3 url from soundcloud api for use with soundmanager2 360-player - Stack Overflow

programmeradmin5浏览0评论

My question is identical to this one here, however as the question was never resolved and I can't message PeeHaa privately to ask the answer I'll ask again here.

I've successfully queried the soundcloud api and gotten my the stream_url for each track and added it to all the hrefs of my 360 player (demo here). My code for the elements looks something like this:

<div class="ui360">
 <a href="/path/to/an.mp3" data-title="Track title">play "an.mp3"</a>
</div>

I have this script to find all the a tags with data-title attributes and give them the correct href based on the stream_url, but when I try to click the soundmanager icon in the browser and play the track, it just gets redirected to the .mp3 url.

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

SC.get('/playlists/2450655', 'allow_redirects=False', function(playlist) {

  var titles = $('.audio .ui360 a').each(function(index){
    this.index = index;
  });

  for (var i = 0; i < playlist.tracks.length; i++) {    
    var id = '?client_id=MY_CLIENT_ID'    
    for (var j = 0; j < titles.length; j++) {      
      if (playlist.tracks[i].title === $(titles[j]).attr('data-title')) {
        $(titles[j]).attr('href', playlist.tracks[i].stream_url+id)
      }            
    }    
  }

});

Does anyone know how to get the correct url for playing tracks hosted on soundcloud in this manner?

My question is identical to this one here, however as the question was never resolved and I can't message PeeHaa privately to ask the answer I'll ask again here.

I've successfully queried the soundcloud api and gotten my the stream_url for each track and added it to all the hrefs of my 360 player (demo here). My code for the elements looks something like this:

<div class="ui360">
 <a href="/path/to/an.mp3" data-title="Track title">play "an.mp3"</a>
</div>

I have this script to find all the a tags with data-title attributes and give them the correct href based on the stream_url, but when I try to click the soundmanager icon in the browser and play the track, it just gets redirected to the .mp3 url.

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

SC.get('/playlists/2450655', 'allow_redirects=False', function(playlist) {

  var titles = $('.audio .ui360 a').each(function(index){
    this.index = index;
  });

  for (var i = 0; i < playlist.tracks.length; i++) {    
    var id = '?client_id=MY_CLIENT_ID'    
    for (var j = 0; j < titles.length; j++) {      
      if (playlist.tracks[i].title === $(titles[j]).attr('data-title')) {
        $(titles[j]).attr('href', playlist.tracks[i].stream_url+id)
      }            
    }    
  }

});

Does anyone know how to get the correct url for playing tracks hosted on soundcloud in this manner?

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Oct 17, 2012 at 1:46 satyrsynthsatyrsynth 4191 gold badge5 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I know this is an old question, but the problem was driving me up the wall and I finally figured it out, so here's my answer for others:

Soundmanager will work if you specify an mp3 mimetype in the link:

<div class="ui360">
    <a type="audio/mp3" href="https://api.soundcloud./tracks/49349198/stream?client_id=YOUR_CLIENT_ID" data-title="Track title">play "an.mp3"</a>
</div>

Why not just use the SoundCloud JavaScript SDK to stream the tracks? Something like:

SC.get('/playlists/2450655', function(playlist) {
  $(playlist.tracks).each(function(index, track) {
    $('a[href="' + track.stream_url + '"]').click(function(e) {
       e.preventDefault();
       SC.stream('/tracks/' + track.id, function(sound) {
         sound.play();
       });
    });
  });
});

Would that solve your problem, or is there a particular reason you need to resolve the URL?

发布评论

评论列表(0)

  1. 暂无评论