事实证明是正确的开始。错误在其他地方,我忘记嵌入字体:/
尽管如此,对于那些构建类似东西的人来说,这里是完整的代码。我也不确定我停止轨道的方法是否正确,所以欢迎任何改进:)
AS3:
import flash.external.ExternalInterface;
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;
function playTrack(track_name) {
var s:Sound = new Sound();
soundName.text =track_name;
track_name = '/path/to/mp3/' + track_name + ".mp3";
var req:URLRequest = new URLRequest(track_name);
var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
s.load(req, context);
s.play();
}
function stopAllTracks() {
SoundMixer.stopAll();
}
ExternalInterface.addCallback("playTrack", playTrack);
ExternalInterface.addCallback("stopAllTracks", stopAllTracks);
在 jQuery 中:
// play a song
$.curPlay = '';
$(".song_play").click(function() {
var id = $(this).attr('id');
song = id.replace('play_', '');
if(song==$.curPlay){ // stop the current song if the play btn is clicked twice
$.stop_mp3();
$.curPlay = '';
return;
}
$.curPlay = song;
$.play_mp3(song);
});
$.play_mp3 = function(file){
$.stop_mp3();
$.talkToFlash("interfaceSounds").playTrack(file);
}
$.stop_mp3 = function(){
$.talkToFlash("interfaceSounds").stopAllTracks();
}
// movie referencer
$.talkToFlash = function(swfFile){
if (window.document[swfFile]) {
return window.document[swfFile];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1){
if (document.embeds && document.embeds[swfFile])
return document.embeds[swfFile];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(swfFile);
}
}
和 HTML/PHP:
<div class='song_play' id='play_<?=$songID?>'>play</div>
我还有一个可能值得分享的技术,因为我发现这在使用 Flash 开发时非常有用。如果您在 PHP 中设置一些配置 Flash 的变量,则可以轻松更改容器的外观或爆破缓存:
<?
$swf = "mp3_player.swf?".time();
$w = 1;
$h = 1;
$mp3Player = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8\"
codebase = \"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\"
width=\"$w\" height=\"$h\" id=\"interfaceSounds\" align=\"middle\">
<param name=\"allowScriptAccess\" value=\"always\" />
<param name=\"allowFullScreen\" value=\"false\" />
<param name=\"movie\" value=\"/assets/swf/$swf\" />
<param name=\"quality\" value=\"high\" />
<embed src=\"/assets/swf/$swf\" quality=\"high\" width=\"$w\"
height=\"$h\" name=\"interfaceSounds\" align=\"middle\"
allowScriptAccess=\"always\" allowFullScreen=\"false\"
type=\"application/x-shockwave-flash\"
pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />
</object>";
稍后在您的 php 文档中,您只需在任何您想要的地方回显它:
<?= $mp3Player ?>