2016-05-17 54 views
0

私はJavaScriptを使用してMP3ファイルを再生します。library状況が変わったため、私は定期的に再生できないファイルにリンクしています。これが起こると、ファイルを解読できないことをFirebugに伝えるメッセージが表示されます。ブラウザでメディアファイルを再生できないようにするにはどうすればいいですか?

メディアリソースhttps://example.com/bad_file.mp3をデコードできませんでした。

screen capture of firebug message concerning bad file
Click here to see the behavior in action on jsfiddle

私はファイルを置き換えるためにできることは何もないので、私は、ファイルが再生できるか否かを判断する方法を探しています。このフレームワークでは、スクリプト自体のロードに失敗した場合にはonerror()メソッドが提供されますが、ファイルを再生できない場合は何もありません。

SoundManager2に固有の回答は受け入れられますが、特定のライブラリやフレームワークとは独立したソリューションが望ましいと思います。あなたはAudioインスタンスへの参照を取得することができれば

function handleSourceError(e) { alert('Error loading: '+e.target.src) } 
function handleMediaError(e) { 
    switch (e.target.error.code) { 
    case e.target.error.MEDIA_ERR_ABORTED: 
     alert('You aborted the media playback.'); break; 
    case e.target.error.MEDIA_ERR_NETWORK: 
     alert('A network error caused the media download to fail.'); break; 
    case e.target.error.MEDIA_ERR_DECODE: 
     alert('The media playback was aborted due to a corruption problem or because the media used features your browser did not support.'); break; 
    case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED: 
     alert('The media could not be loaded, either because the server or network failed or because the format is not supported.'); break; 
    default: 
     alert('An unknown media error occurred.'); 
    } 
} 

var toArray = Array.prototype.slice; 
toArray.apply(document.getElementsByTagName('audio')).forEach(function(audio){ 
    audio.addEventListener('error', handleMediaError); 
    toArray.apply(audio.getElementsByTagName('source')).forEach(function(source){ 
    source.addEventListener('error', handleSourceError); 
    }); 
}); 

、あなたはエラーハンドラを追加することができます:あなたは標準<audio src="whatever.mp3">表記法を使用していた場合

答えて

0

一般に、リソースをロードする要素にonerrorイベントリスナーを追加できます。だから、例えば、あなたがオーディオ要素SoundManager2については

//assume this html: 
//<audio id="myaudio" src="http://badurl.com/mp3.mp3"></audio> 
document.getElementById("myaudio").addEventListener("error",function(event){ 
    //do error handling here 
}); 

にイベントリスナーを付けるだろうオーディオをやっているので、特にあなたがonloadイベントのコールバックオプションを渡すことができ、それはsuccess変数が渡されます、真のそれは偽のロードのためにそうでない場合はAPI reference

var mySound = soundManager.createSound({ 
    id: 'aSound', 
    url: 'http://badurl.com/mp3.mp3', 
    onload:function(success){ 
    if(!success){ 
     //error happened 
    } 
    } 
}); 
mySound.play(); 
+0

私がプログラムでサウンドを作成する場合にのみ機能します。私がSoundManager2に頼って私のmp3リンクを見つけて、それらのためのプレーヤーを作るなら、私はそのパラメータにアクセスすることはできません。 – Keyslinger

+0

@Keyslinger次に、onloadイベントを使用するように[defaultOptions](http://www.schillmania.com/projects/soundmanager2/doc/#smdefaults)を変更します: 'soundManager.defaultOptions.onload = function(){}' –

0

あなたが発生したすべてのオーディオ・エラーにフックするこのスクリプトを使用することができます

関連する問題