【问题标题】:PHP/jQuery - how to play mp3 audio file using jQuery when PHP statement is truePHP/jQuery - 当 PHP 语句为真时如何使用 jQuery 播放 mp3 音频文件
【发布时间】:2014-02-07 19:31:29
【问题描述】:

我有一个简单的钓鱼“游戏”,需要知道如何在 PHP 语句为真时使用 jQuery 播放 mp3 文件。

jQuery 是

<script>
    $(document).ready(function() {
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', 'audio/TEST.mp3');
        audioElement.setAttribute('autoplay', 'autoplay');
        //audioElement.load()

        $.get();

        audioElement.addEventListener("load", function() {
            audioElement.play();
        }, true);

        $('.play').click(function() {
            audioElement.play();
        });

        $('.pause').click(function() {
            audioElement.pause();
        });
    });
</script>

但我需要在 if(empty($caught)) { $Output=... 为真时运行它。所以当这是真的,或者当 $Output 被回显时,声音文件会自动播放。

该文件目前确实可以播放,只是当 PHP 语句为真时不会播放。

完整的相关代码如下。我只是不知道如何使它工作。

<form action="fishing.php" method="post">
<?php
$fish = array('' => '', 'coelacanth.gif' => 'coelacanth', 'seadragon.gif' => 'sea dragon', 'rainbow_trout.gif' => 'rainbow trout', 'dolphinfish.gif' => 'dolphinfish', 'catfish.gif' => 'catfish', 'monkfish.gif' => 'monkfish', 'lmbass.gif' => 'large-mouth bass', 'smbass.gif' => 'small-mouth bass', 'shiner.gif' => 'shiner', 'perch.gif' => 'perch', 'frogfish.gif' => 'frogfish', 'pickerel.gif' => 'pickerel', 'minnow.gif' => 'minnow', 'flying_fish.gif' => 'flying fish', 'clownfish.gif' => 'clownfish', 'tropical_fish.gif' => 'tropical fish', 'betta.gif' => 'betta', 'shark.gif' => 'shark', 'octopus.gif' => 'octopus', 'koi.gif' => 'koi', 'sunfish.gif' => 'sunfish');
$rand = array_rand($fish);

$Caught = $fish[$rand];
$Output = '';

if(empty($Caught)){
    $Output = 'Uh-oh, looks like that one got away!';
    echo $Output;
} else {
    echo '<li class="fish_pic"><img src="img/'.$rand.'" alt="'.$fish[$rand].'" title="'.$fish[$rand].'" class="fish"></li>'."\n";
    echo '<p class="caught">' . "Nice one! You caught a " . $fish[$rand] . "!</p>";
    }
?>
</p>
<p><input type="submit" class="btn btn-primary" value="Go Fishing!"></p>
</form>

<div class="clearfix"></div>
</div><!-- close .starter-template -->
</div><!-- /.container -->

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>

<script>
    $(document).ready(function() {
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', 'audio/TEST.mp3');
        audioElement.setAttribute('autoplay', 'autoplay');
        //audioElement.load()

        $.get();

        audioElement.addEventListener("load", function() {
            audioElement.play();
        }, true);

        $('.play').click(function() {
            audioElement.play();
        });

        $('.pause').click(function() {
            audioElement.pause();
        });
    });
</script>

提前感谢您能给我的任何帮助;我很困惑,已经用谷歌搜索了这个问题。

【问题讨论】:

    标签: php jquery audio mp3


    【解决方案1】:

    如果满足条件,则可以输出脚本;y

    if(empty($Caught)){
        $Output = 'Uh-oh, looks like that one got away!';
        echo $Output;
    ?>
    <script>
        $(document).ready(function() {
            var audioElement = document.createElement('audio');
            audioElement.setAttribute('src', 'audio/TEST.mp3');
            audioElement.setAttribute('autoplay', 'autoplay');
            //audioElement.load()
    
            $.get();
    
            audioElement.addEventListener("load", function() {
                audioElement.play();
            }, true);
    
            $('.play').click(function() {
                audioElement.play();
            });
    
            $('.pause').click(function() {
                audioElement.pause();
            });
        });
    </script>
    <?php
    } else {
        echo '<li class="fish_pic"><img src="img/'.$rand.'" alt="'.$fish[$rand].'" title="'.$fish[$rand].'" class="fish"></li>'."\n";
        echo '<p class="caught">' . "Nice one! You caught a " . $fish[$rand] . "!</p>";
    }
    

    【讨论】:

    • 现在看起来很明显。唯一的就是 audioElement.play();和 $('.play').click(function() { audioElement.play(); }); .....我可以删除它吗?这会阻止它自动播放吗?
    • @bealstreetmedia 它不应该
    • 我输入的代码和上面完全一样,但它没有播放...有什么想法吗?
    • 让脚本自行运行没有问题。当我在 PHP 中插入脚本时它不起作用。 FWIW小提琴没有为我演奏任何东西......?当我直接访问源 URL 时,它确实有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多