0

我是 PHP 新手,目前正在这个网页上工作,该网页读取文件夹中的许多音频文件,并让用户编写关于她/他所听到内容的文字记录。目前我可以读取目录中的文件并打乱该数组。我还想实现两个按钮,让用户收听下一个/上一个文件。那就是我现在遇到问题的地方。下一个按钮似乎正在工作,但不是我想要的。问题是我只能移动指针一次。我的意思是我可以从第一个文件到第二个文件,我不能再前进了。这是用户单击按钮时调用的函数的代码: function nextelem(){

            var nextElement = "./Sound data/<?php


            $current_id = current($files);
            $current_index = array_search($current_id, $files);     
            $next_index = $current_index+1;

            echo $files[$next_index];

            next($files);

            ?>";

            //Play the next file
            document.getElementById('RandomAudio').src=nextElement;


          }

我猜问题是 current($array) 和 next($array) 函数是按值原则工作的,所以每次调用函数时,都会传递原始数组的副本,所以每次按钮单击我将第一个元素作为当前元素并使用 next($array) 移动指针并没有真正的效果。

所以我尝试编写自己的 current($array) 和 next($array) 函数:

        <?php
               function &current_by_ref(&$arr) {
                    return $arr[key($arr)];
                }
                function &next_by_ref(&$arr) {
                    return $arr[key($arr)+1];
                }
        ?>;

但这些也无济于事。我真的很感激任何帮助或提示。这开始让我紧张了。(PS:我打开了一个关于(同一个项目但不是同一个问题)的主题,这真的很有帮助。链接在这里PHP next($array) 和 Javascript。我在那里发布了所有代码,它不是最后一个我现在正在处理的版本,但它可以让你对我认为的页面有更多的了解。所以还是谢谢)

根据氟利昂的回答,我将代码更改为:

               session_start();
                $dir = './Sound data';
                $files = scandir($dir); 


                    $norp = $_GET['name'];  

                    $current_id = current($files);
                    $current_index = array_search($current_id, $files);     
                    $_SESSION['current'] = $current_index;
                    $_SESSION['songlist'] = $files;

                    $current_song = $_SESSION['current']; //index in songlist for your song
                    $songlist = $_SESSION['songlist']; //this session var has your $files array
                    if($norp == 'nextS'){

                        $current_song++; //changes currentsong to next song
                        if($current_song == count($songlist))
                           $current_song = 0;
                        echo "./Sound data/$songlist[$current_song]";
        }           

                    else if($norp == 'prevS'){
                        $current_song--;     //changes currentsong to prev song

                        if($current_song == count($songlist))
                        $current_song = 0;
                        echo "./Sound data/$songlist[$current_song]";
                    }

?>

    <script language="JavaScript" type="text/javascript" >

        function getNextSong()
        {                 
            $.get('getsong.php', {name : 'nextS'}, function(song){

                document.getElementById('RandomAudio').src =  song;
                document.getElementById('filename').innerHTML= song;
            //  alert(song);
            });
        }

        function getPreviousSong()
        {                 
            $.get('getsong.php', {name : 'prevS'}, function(song){

                document.getElementById('RandomAudio').src =  song;
                document.getElementById('filename').innerHTML= song;
            //  alert(song);
            });
        }
    </script>

但我仍然无法超越下一个元素。难道我做错了什么?

4

1 回答 1

2

您正在混合 PHP 和 JavaScript。你不能那样做。代码将被解释,您将只有下一个文件可用。您的输出可能是:

var nextElement = "./Sound data/2.mp3";
   document.getElementById('RandomAudio').src=nextElement;

在您第二次按下按钮时,将执行相同的代码。这就是为什么你不能走得更远。您必须找到一种从服务器检索下一个音频的方法,为此您必须跟踪正在播放的音频。

歌颂.php

<?php
session_start(); //init session

if(!isset($_SESSION['songlist']) or empty($_SESSION['songlist']))
{ //we'll create our songlist JUST ONCE, when it's not created yet. And then, we'll put this list in a SESSION, so we can access it later.
    $dir = './Sound data';
    $files = scandir($dir);
    $_SESSION['songlist'] = array();
    foreach($files as $file)
    {//loop through song list and just add actual files, disposing "." and ".."
        if(is_file($file) and $file != "." and $file != "..")
        {
            $_SESSION['songlist'][] = $file; //add $file to songlist array.
        }
    }
    shuffle($_SESSION['songlist']);//shuffle list
    $_SESSION['current'] = 0; //iniate current song with first song in the list.
}//note that we'll create and shuffle the song list just once.

$norp = $_GET['name'];

$current_song = $_SESSION['current']; //index in songlist for your song
$songlist = $_SESSION['songlist']; //this session var has your $files array

if($norp == 'nextS'){
    $current_song++; //changes currentsong to next song
    if($current_song == count($songlist)) //if current is after the last element, set it to the first song
        $current_song = 0;
    echo "./Sound data/$songlist[$current_song]";
}
else if($norp == 'prevS'){
    $current_song--;     //changes currentsong to prev song
    if($current_song == -1) //if current is before the fist song, set it to the last song
        $current_song = count($songlist) - 1;

    echo "./Sound data/$songlist[$current_song]";
}

$_SESSION['current'] = $current_song; //store current song for later use.
?>

listensong.php //page that plays your songs(带有代码优化)

<script type="text/javascript">
    //using jQuery
    function getSong(type)
    {
        $.get('getsong.php', {name: type}, function(song){
            document.getElementById('RandomAudio').src = song;
            document.getElementById('filename').innerHTML= song;
        });
    }
</script>
<a href="javascript:getSong('prevS')">Previous Song</a>
<a href="javascript:getSong('nextS')">Next Song</a>
于 2012-11-01T17:19:23.520 回答