如何使用php和html中的按钮从数据库播放音频?

时间:2016-06-29 19:35:58

标签: javascript php html audio html5-audio

我试图用里面的音频播放器构建我的网站。但是当我把数据库加载的歌曲和播放按钮结合起来时,我有问题。我想在点击并从数据库加载歌曲时更改我的播放按钮。我有这样的代码:

HTML和PHP

<div id="playbtn">              
   <button id="play_btn" onclick="playPause()"></button>                    
      <?php
          $song= "SELECT mp3Lagu FROM folksong WHERE songtitle = 'Apuse'";
          $result = mysql_query($song);

          while ($row = mysql_fetch_array($result)) { 
              echo'
                 <audio id="listenlagu">
                 <source src="data:audio/mp3;base64,'.base64_encode( $row['mp3Lagu'] ).'">
                 </audio>';
          }
      ?></div>

我使用javascript更改显示按钮,如下所示:

JAVASCRIPT

<script>
var audio, playbtn;
function initAudioPlayer(){
audio = new Audio();
//audio = document.getElementById('listenlagu');
//audio.src = "audio/Apuse.mp3";
audio.src = document.getElementById('listenlagu');  
audio.load();
audio.loop = true;
audio.play();

// Set object references
playbtn = document.getElementById("play_btn");

// Add Event Handling
playbtn.addEventListener("click",playPause);

// Functions
function playPause(){
    if(audio.paused){
        audio.play();
        playbtn.style.background = "url(images/pause70.png) no-repeat";
    } else {
        audio.pause();
        playbtn.style.background = "url(images/play70.png) no-repeat";
    }
}   
}
window.addEventListener("load", initAudioPlayer);
</script>

但是当我结合使用javascript -_____-&#34;

时,它无法正常工作

任何人都知道问题出在哪里? 你能帮我解决这些问题吗?

1 个答案:

答案 0 :(得分:0)

您应该更新以下内容(下面的完整代码示例):

  1. 将src属性分配给audio.src
  2. 将你的playPause函数移到initAudioPlayer函数之外
  3. 让你的playPause函数接受2个参数1 - &#34; audio&#34; (新的Audio对象)和2 - playbtn(按钮元素),并让它返回一个函数,以便它可以用作addEventListenter方法中的回调
  4. 这是一个有效的傻瓜:https://plnkr.co/edit/bticzS?p=preview

    #make a cylinder without the ends closed
    import numpy as np
    from matplotlib import cm
    from matplotlib import pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from scipy.linalg import norm
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection
    import numpy as np
    import math
    
    
    
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    
    
    origin = [0,0,0]
    #radius = R
    p0 = np.array(origin)
    p1 = np.array([8, 8, 8])
    origin = np.array(origin)
    R = 4
    
    #vector in direction of axis
    v = p1 - p0
    #find magnitude of vector
    mag = norm(v)
    #unit vector in direction of axis
    v = v / mag
    #make some vector not in the same direction as v
    not_v = np.array([1, 0, 0])
    if (v == not_v).all():
        not_v = np.array([0, 1, 0])
    #make vector perpendicular to v
    n1 = np.cross(v, not_v)
    #normalize n1
    n1 /= norm(n1)
    #make unit vector perpendicular to v and n1
    n2 = np.cross(v, n1)
    #surface ranges over t from 0 to length of axis and 0 to 2*pi
    t = np.linspace(0, mag, 600)
    theta = np.linspace(0, 2 * np.pi, 100)
    #use meshgrid to make 2d arrays
    t, theta = np.meshgrid(t, theta)
    #generate coordinates for surface
    X, Y, Z = [p0[i] + v[i] * t + R * np.sin(theta) * n1[i] + R * np.cos(theta) *    n2[i] for i in [0, 1, 2]]
    
    
    #make the color for the faces
    col1 = plt.cm.autumn(np.ones(600)) # linear gradient along the t-axis
    col1 = np.repeat(col1[np.newaxis,:, :], 100, axis=0) # expand over the theta-axis
    
    
    
    ax.plot_surface(X, Y,Z, facecolors = col1, shade = True,edgecolors = "None",    alpha = 0.4, linewidth = 0)
    
    plt.show()