就像它在標題中所說的,我試圖爲我的網站製作MP3播放器,並且我遇到了一些麻煩。我的代碼看起來是這樣的(這是在測試和開發階段,所以,儘量不要關注有關發展階段的東西)...as3 mp3播放器無法加載一些mp3文件
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
// First run
if (songPlaying == undefined) {
var songPlaying = '0';
var songPause = '0';
var songLoadingCompleted = '1';
var prevSongURL:Array;
var currentSongURL:Array;
var nextSongURL:Array;
var soundFile:URLRequest;
var currentSong:Sound;
var channel:SoundChannel;
currentSongURL = ['','',''];
trace("Player defaults loaded.");
}
// Testing vars
nextSongURL = ['1','NEXT','http://www.balkanize.me/dll/dl5/Sasa_Kovacevic_Slucajno.mp3'];
prevSongURL = ['3','PREV','http://tm-alonso.perso.sfr.fr/Musique/NRJ%20Music%20Awards%202012/CD1/06%20-%20Pitbull%20feat.%20Chris%20Brown%20-%20International%20Love.mp3'];
//soundFile = new URLRequest(lastPlayedSongURL[2]);
//currentSong = new Sound();
//channel = new SoundChannel();
trace("Testing values loaded.");
//JS communication
//Get next next file
function getNextSong(lastPlayed:Array) {
//NOT FINISHED
trace("Loading next track");
return nextSongURL;
}
//Get previous next file
function getPrevSong(lastPlayed:Array){
//NOT FINISHED
trace("Loading prev track");
return prevSongURL;
}
//FUNCTIONS
function songLoadingProgress(evt:ProgressEvent):void{
var loadedPct:uint = Math.round(100 * (evt.bytesLoaded/evt.bytesTotal));
trace("The sound is " + loadedPct + "% loaded.");
}
function songLoadingComplete(evt:Event):void{
trace("The sound is completely loaded.");
songLoadingCompleted = '1';
}
function onIOError(evt:IOErrorEvent){
trace("The sound could not be loaded: " + evt.text);
}
// Player controls
function playSong(evt:MouseEvent):void {
if (songPlaying !== '1') {
info.text = "PLAY!";
if (currentSongURL[2] == '') {
currentSongURL = getNextSong(currentSongURL);
}
trace("Trying to play:" + currentSongURL[0] + " - " + currentSongURL[1] + " - " + currentSongURL[2]);
if (songLoadingCompleted !== '1') {
currentSong.close();
currentSong = new Sound();
}
songLoadingCompleted = '0';
soundFile = new URLRequest(currentSongURL[2]);
currentSong = new Sound();
channel = new SoundChannel();
currentSong.load(soundFile);
channel = currentSong.play(songPause);
currentSong.addEventListener(ProgressEvent.PROGRESS, songLoadingProgress);
currentSong.addEventListener(Event.COMPLETE, songLoadingComplete);
currentSong.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
//channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
songPlaying = '1';
songPause = '0';
}
}
function stopSong(evt:MouseEvent):void {
if (songPlaying !== '0') {
trace('STOP!');
songPause = '0';
songPlaying = '0';
channel.stop();
return;
}
}
function playNext(evt:MouseEvent):void {
if (songPlaying !== '0') {
trace('STOP!');
songPlaying = '0';
channel.stop();
}
currentSongURL = getNextSong(currentSongURL);
playSong(null);
}
function playPrev(evt:MouseEvent):void {
if (songPlaying !== '0') {
trace('STOP!');
songPlaying = '0';
channel.stop();
}
currentSongURL = getPrevSong(currentSongURL);
playSong(null);
}
btnPrev.addEventListener(MouseEvent.CLICK,playPrev);
btnPlay.addEventListener(MouseEvent.CLICK,playSong);
// btnPause.addEventListener(MouseEvent.CLICK,pauseSong);
btnStop.addEventListener(MouseEvent.CLICK,stopSong);
btnNext.addEventListener(MouseEvent.CLICK,playNext);
問題是,我的網頁瀏覽器,以及一些其他的閃存MP3玩家可以打開「http://www.balkanize.me/dll/dl5/Sasa_Kovacevic_Slucajno.mp3」,我的玩家不能。然而,其他文件,'http://tm-alonso.perso.sfr.fr/Musique/NRJ%20Music%20Awards%202012/CD1/06%20-%20Pitbull%20feat.%20Chris%20Brown%20-%20International%20Love.mp3',可以打開,播放,重播,無論如何。
我注意到我無法打開的文件有一些不同的標題(它沒有內容長度標題)。但是,我再次嘗試了一些沒有內容長度標題的文件,並且可以打開它。
我不知道我知道我錯在哪裏......:D幫助,並提前致謝! :)
蘇珊
嗯,這可能是可能的,但是,我的網站現在有超過80.000個文件,並且我從其他網站加載了大約1.500.000個文件,並且我無法控制這1.5百萬個文件。我正在與這些文件的持有者合作,但我懷疑他們會很樂意爲我處理所有文件... 此外,正如我所提到的,其他的MP3播放器,如JWPlayer(測試它!),正在加載每一個文件,從我所有的來源。 有沒有比URLRequest或.load()加載文件更好的方法?如果JW能夠做到這一點,那麼我,你也可以做到,做到這一點的方式是真正的問題......;) –