WEBデザインの勉強

初心者が1からWEBデザインを勉強するブログ。

HTML5のaudioやvideoの埋め込み・再生ができるjQueryプラグインjPlayer

f:id:mmmmofu:20170510232911p:plain

公式サイト
jplayer.org

デモ
http://efu.sakura.ne.jp/web/jplayer/
※音が流れます

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>jPlayer</title>
  <link rel="stylesheet" href="http://efu.sakura.ne.jp/web/common/css/reset.css">
  <link rel="stylesheet" href="http://efu.sakura.ne.jp/web/common/css/font-awesome.min.css">
  <link rel="stylesheet" href="css/style.css">
  <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
  <script src="js/jquery.jplayer.js"></script>
  <script src="js/script.js"></script>
</head>
<body>
  <div class="sound">
    <div class="sound_icon">
      <i class="fa fa-play-circle play" aria-hidden="true"></i>
      <i class="fa fa-pause-circle pause" aria-hidden="true"></i>
    </div>
  </div>
  <div id="audio"></div>
  <ul class="soundlist">
    <li class="sound1">
      <img src="img/icon01.png" alt="">
      <div class="play1"></div>
      <div class="pause1"></div>
      <div id="audio1"></div>
    </li>
    <li class="sound2">
      <img src="img/icon02.png" alt="">
      <div class="play2"></div>
      <div class="pause2"></div>
      <div id="audio2"></div>
    </li>
  </ul>
</body>
</html>
body {
  text-align: center;
}

.sound {
  position: relative;
  display: inline-block;
  margin: 50px 0;
  cursor: pointer;
}

.sound_icon i {
  font-size: 83px;
  -webkit-transition: all .3s linear;
  -o-transition: all .3s linear;
  transition: all .3s linear;
}

.fa-pause-circle {
  display: none;
  color: #D0DD97;
}

.fa-pause-circle:hover {
  color: #658361;
}

.fa-play-circle {
  color: #F49393;
}

.fa-play-circle:hover {
  color: #F21368;
}

.soundlist {
  font-size: 0;
  text-align: center;
}

.soundlist li {
  position: relative;
  display: inline-block;
  margin-left: 50px;
  cursor: pointer;
}

.soundlist li:first-child {
  margin-left: 0;
}

.soundlist li .play1, .soundlist li .play2, .soundlist li .pause1, .soundlist li .pause2 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
$(function(){
  $('.sound').on('click', function(){
    $('#audio').jPlayer({
      ready: function () {
        $(this).jPlayer("setMedia", {
          mp3: "mp3/sound.mp3"
        }).jPlayer('play');
      },
      loop: false,
      volume: 0.5,
      ssupplied: 'mp3',
      cssSelectorAncestor: '.sound',
      cssSelector: {play: '.play',pause: '.pause'},
      play: function(){
        console.log('play');
      },
      pause: function(){
        console.log('pause');
      },
      ended: function(){
        console.log('ended');
      }
    });
  });

  $('.soundlist li').on('mouseenter', function(){
    var current = $(this).index() + 1;
    $('#audio' + current).jPlayer({
      ready: function () {
        $(this).jPlayer("setMedia", {
          mp3: "mp3/sound" + current + ".mp3"
        }).jPlayer('play');
      },
      loop: false,
      volume: 0.5,
      ssupplied: 'mp3',
      cssSelectorAncestor: '.sound',
      cssSelector: {play: '.play' + current, pause: '.pause' + current}
    });
  }).on('mouseleave', function(){
    var current = $(this).index() + 1;
    $('#audio' + current).jPlayer("destroy");
  });
});

素材お借りしました。
www.hmix.net