【问题标题】:Play SoundCloud Stream with Custom Button使用自定义按钮播放 SoundCloud Stream
【发布时间】:2017-03-08 19:01:22
【问题描述】:

我在使用自定义按钮播放 SoundCloud 流时遇到问题。

这是我正在开发的 CodePen:https://codepen.io/tremolocreative/pen/Zepjwm

HTML

<script src="https://connect.soundcloud.com/sdk.js"></script>
<audio id="soundcloudPlayer"></audio>
<button></button>

CSS

button {
  background: url(//cdn.shopify.com/s/files/1/1055/5530/t/8/assets/play-pause-sprite.svg?2157621096199230646);
  background-size: cover;
  background-position: top left;
  background-repeat: no-repeat;
  width: 20px;
  height: 20px;
  border: 0;
  outline: 0;
  display: block;
}

button.pause-sprite {
  background-position: top right;
}

JS

var client_id = '278594df9a311b2a1a56251b3a2b0fbe';
var track_id = '293605256';

SC.initialize({
  client_id: client_id
});

SC.get('/tracks/' + track_id, {}, function(sound) {
  $('#soundcloudPlayer').attr('src', sound.stream_url + '?client_id=' + client_id);
  $('button').on('click', function() {
    $(this).toggleClass('pause-sprite');   

    $('#soundcloudPlayer').play(); // Play track

  });
});

谢谢!

【问题讨论】:

  • 你在哪里将声音传递给函数?控制台只是将其显示为对象 Object.. 似乎没有收到该值。
  • 这就是我有点不确定...我尝试遵循 API 文档但无法使其工作... SC.stream('/tracks/293').then (函数(播放器){ player.play(); }); developers.soundcloud.com/docs/api/sdks#streaming
  • 我有一个工作小提琴,但要切换到暂停精灵,你必须添加一个切换/如果播放然后暂停并添加暂停类..
  • 谢谢@RachelGallen!
  • 这是小提琴,jsfiddle.net/RachGal/gjh8dyny 我尝试添加暂停,基于此功能 view-source:rachelgallen.com/workingaudio.html 但它并没有像我想象的那样工作。我稍后再去,但我还有一些工作要赶上..

标签: javascript html css soundcloud


【解决方案1】:

这是我的工作 sn-p,虽然它不完整(我正在吃晚饭,在我看到你的评论之前会完成它)基本上剩下的就是添加一个 if-else 语句来查看音频是否正在播放,如果它正在播放,则暂停它。在暂停时添加暂停类(但如果存在,则在播放时将其删除)。

目前,在fiddle 中,图标切换为暂停,但播放没有暂停.. 在 sn-p 中,我放置了一个不会切换但更接近 else-if 的替代版本(我还没看API)

var client_id = '278594df9a311b2a1a56251b3a2b0fbe';
var track_id = '293605256';

SC.initialize({
  client_id: client_id
});

$(document).ready(function() {
  $("#stream").on("click", function() {
    SC.stream("/tracks/" + track_id, function(sound) {
      if (sound.currentTime > 0) {
        $('button').addClass('pause-sprite');
        sound.pause();
      } else {
        $('button').removeClass('pause-sprite');
        sound.play();
      }
    });
  });
});
button {
  background: url(//cdn.shopify.com/s/files/1/1055/5530/t/8/assets/play-pause-sprite.svg?2157621096199230646);
  background-size: cover;
  background-position: top left;
  background-repeat: no-repeat;
  width: 20px;
  height: 20px;
  border: 0;
  outline: 0;
  display: block;
}

button.pause-sprite {
  background-position: top right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//connect.soundcloud.com/sdk-2.0.0.js"></script>
<div id='player'>
  <button href="#" id="stream">

  </button>
</div>

【讨论】:

  • 理想情况下,如果可能,您应该添加一个“onended”事件侦听器,并将暂停/播放图标更改为在完成后停止。否则,结束时将其从暂停改回播放..只是一个想法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-01
  • 2013-06-22
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
相关资源
最近更新 更多