使用Rails SDK实现Soundcloud播放器

时间:2013-04-29 19:41:28

标签: ruby-on-rails soundcloud

我是新手rails开发人员,我正在尝试用rails和soundcloud做一件简单的事情,但似乎在几天后乱用API文档无法找到做我想要的最好方法。

我正在尝试为当地的酒吧开设一个比赛,DJ可以向我们发送他们最好的赛道并且他们被选中赢得奖品。

我对此的近似是将其轨道的soundcloud url存储在数据库字段中,然后通过小型soundcloud播放器在网络中显示这些轨道。

我想我可以在视图文件中使用此代码。

<div id="player">
<object height="70" width="100%">
  <param name="movie" value="https://player.soundcloud.com/player.swf?    url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F{"<%= @trackid %>"}&player_type=tiny{widgetParams}">    </param>
  <param name="allowscriptaccess" value="always"></param>
  <param name="wmode" value="transparent"></param>
  <embed wmode="transparent" allowscriptaccess="always" height="18" width="100%"     src="https://player.soundcloud.com/player.swf?    url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F{trackId}&player_type=tiny{widgetParams}">    </embed>
</object> 
</div>

使用track.id的值更改{trackid}即时消息,我在控制器中使用此代码获取

 client = Soundcloud.new(:client_id => '2074955755d1c3997e10463d8a56960f')

# call the resolve endpoint with a track url
track = client.get('/resolve', :url => "http://soundcloud.com/forss/flickermood")

# print the track id
@trackid = track.id

这不起作用。

我尝试做这项工作的另一种方法是通过oEmbed。我试图在API文档中运行该示例。

在View文件中,我知道我必须放置Javascript Widget:

<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
SC.initialize({
  client_id: 'xxxxxxxxxxxxxxxxxxxxxxx'
});

var track_url = 'http://soundcloud.com/forss/flickermood';
SC.oEmbed(track_url, { auto_play: true }, function(oEmbed) {
  console.log('oEmbed response: ' + oEmbed);
});
</script>

在控制器中这段代码:

# create a client object with your app credentials
client = Soundcloud.new(:client_id => 'xxxxxxxxxxxxxxxxxxxx')

# get a tracks oembed data
track_url = 'http://soundcloud.com/forss/flickermood'
embed_info = client.get('/oembed', :url => track_url)

# print the html for the player widget
puts embed_info['html']

我有点失落,我认为我在一些基本的事情上失败了,这就是为什么我找不到有关如何实现这一点的信息。

感谢您的时间

1 个答案:

答案 0 :(得分:2)

你视线中的这一行跳出来了我:

<param name="movie" value="https://player.soundcloud.com/player.swf?    url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F{"<%= @trackid %>"}&player_type=tiny{widgetParams}">    </param>

@trackid周围的双引号会切断值参数并弄乱HTML。试试这个,然后检查您的视图生成的HTML:

<param name="movie" value="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F<%= @trackid %>&player_type=tiny{widgetParams}"></param>