【发布时间】:2019-08-19 12:22:21
【问题描述】:
一个教育游戏使用一组 25 个以数字为名称的 mp3 文件(1.mp3、2.mp3、3.mp3 等)。它们一个接一个地播放,并在主声音中随机添加一个音频前缀。
我使用这个帖子:Merge two mp3 php 来合并文件。现在我为每个播放的音轨准备了三个 mp3 文件:主声音文件(1.mp3、2.mp3 等...)和两个辅助文件 s_prefix.mp3(声音被添加)和 s_blank.mp3 (添加静音)。我用这些辅助文件创建了一个数组,将它们随机化并添加到主音频之前。
<?php
$a = $b = $c = 1;
$arr = array("mp3files/prefix.mp3", "mp3files/blank.mp3");
?>
<table>
<tr>
<td><a href="
<?php
// first audio
$s_rand = $arr[rand(0,sizeof($arr)-1)];
file_put_contents('mp3files/comb' . $a++. '.mp3', file_get_contents($s_rand) . file_get_contents('mp3files/' . $b++. '.mp3'));
echo ('mp3files/comb' . $c++. '.mp3');
?>
">Example text1</a></td>
</tr>
<tr>
<td><a href="
<?php
// second audio
$s_rand = $arr[rand(0,sizeof($arr)-1)];
file_put_contents('mp3files/comb' . $a++. '.mp3', file_get_contents($s_rand) . file_get_contents('mp3files/' . $b++. '.mp3'));
echo ('mp3files/comb' . $c++. '.mp3');
?>
">Example text2</a></td>
</tr>
<tr>
<td><a href="
<?php
// third etc... audio
$s_rand = $arr[rand(0,sizeof($arr)-1)];
file_put_contents('mp3files/comb' . $a++. '.mp3', file_get_contents($s_rand) . file_get_contents('mp3files/' . $b++. '.mp3'));
echo ('mp3files/comb' . $c++. '.mp3');
?>
">Example text3</a></td>
</tr>
</table>
它运行良好,但即使在我所知甚少的情况下,它看起来也很笨重并且需要重复很多次。有人可以简化代码还是最好创建一个 php 函数来重申它。
【问题讨论】:
标签: php function file audio merge