HTML5 오디오 404 오류시 다른 파일 재생 편집하기

경고: 로그인하지 않았습니다. 편집을 하면 IP 주소가 공개되게 됩니다. 로그인하거나 계정을 생성하면 편집자가 사용자 이름으로 기록되고, 다른 장점도 있습니다.

편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.

최신판 당신의 편집
7번째 줄: 7번째 줄:
*해당 파일이 없을 때 대신 다른 파일을 재생하고 싶다.
*해당 파일이 없을 때 대신 다른 파일을 재생하고 싶다.


==방법 1: syntaxhighlight 추가==
==방법 1: source 추가==
*audio에 syntaxhighlight를 추가하면 브라우저가 알아서 다음 syntaxhighlight를 재생한다.
*audio에 source를 추가하면 브라우저가 알아서 다음 source를 재생한다.
:테스트환경: (동작함) 크롬 40.0, (동작안함) 인터넷익스플로러 11.0
:테스트환경: (동작함) 크롬 40.0, (동작안함) 인터넷익스플로러 11.0
*예제: http://zetawiki.com/ex/js/audio-error-skip.php
*예제: http://zetawiki.com/js/audio-error-skip.php
<syntaxhighlight lang='html5'>
<source lang='html5'>
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
<script>
22번째 줄: 22번째 줄:
   
   
<audio id="myaudio">
<audio id="myaudio">
<syntaxhighlight src='not-exist.ogg'>
<source src='not-exist.ogg'>
<syntaxhighlight src="http://upload.wikimedia.org/wikipedia/commons/f/fa/B-major.ogg">
<source src="http://upload.wikimedia.org/wikipedia/commons/f/fa/B-major.ogg">
</audio>
</audio>


<button class='play-myaudio'>오디오 재생</button>
<button class='play-myaudio'>오디오 재생</button>
</syntaxhighlight>
</source>


;실행결과 (크롬 40.0 콘솔)
;실행결과 (크롬 40.0 콘솔)
<syntaxhighlight lang='text'>
<source lang='text'>
audio-error-skip.php:1 GET http://zetawiki.com/ex/js/not-exist.ogg  
audio-error-skip.php:1 GET http://zetawiki.com/js/not-exist.ogg  
</syntaxhighlight>
</source>
:→ 오류는 발생하지만 버튼을 눌러보면 B-major.ogg가 재생된다.
:→ 오류는 발생하지만 버튼을 눌러보면 B-major.ogg가 재생된다.


==방법 2: onerror 추가==
==방법 2: onerror 추가==
{{참고|HTML5 오디오 이벤트리스너 onerror}}
{{참고|HTML5 오디오 이벤트리스너 onerror}}
*예제: http://zetawiki.com/ex/js/audio-error-skip2.php
*예제: http://zetawiki.com/js/audio-error-skip2.php
<syntaxhighlight lang='html5'>
<source lang='html5'>
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
<script>
44번째 줄: 44번째 줄:
var myaudio = new Audio();
var myaudio = new Audio();
myaudio.onended = function() {
myaudio.onended = function() {
console.log( '재생 완료' );
console.log( '재생 완료');
}
}
myaudio.onerror = function() {
myaudio.onerror = function() {
console.log( this.src + ' 파일 없어서 대신 POP.WAV를 재생' );
console.log( this.src + ' 파일 없어서 대신 POP.WAV를 재생');
var alternative_src = 'http://free-sounds.net/sound-files/special-effects/POP.WAV';
var alternative_src = 'http://free-sounds.net/sound-files/special-effects/POP.WAV';
if( this.src == alternative_src) {
if( this.src == alternative_src) {
console.log( 'POP.WAV도 없음!' );
console.log('POP.WAV도 없음!');
return;
return;
}
}
67번째 줄: 67번째 줄:
<button onclick="play_audio('not-exist.wav');">not-exist.wav 오디오 재생</button>
<button onclick="play_audio('not-exist.wav');">not-exist.wav 오디오 재생</button>
<button onclick="play_audio('http://free-sounds.net/sound-files/special-effects/ANGELS_F.WAV');">ANGELS_F.WAV 오디오 재생</button>
<button onclick="play_audio('http://free-sounds.net/sound-files/special-effects/ANGELS_F.WAV');">ANGELS_F.WAV 오디오 재생</button>
</syntaxhighlight>
</source>


==같이 보기==
==같이 보기==

제타위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0 라이선스로 배포된다는 점을 유의해 주세요(자세한 내용에 대해서는 제타위키:저작권 문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요.
또한, 직접 작성했거나 퍼블릭 도메인과 같은 자유 문서에서 가져왔다는 것을 보증해야 합니다. 저작권이 있는 내용을 허가 없이 저장하지 마세요!

취소 편집 도움말 (새 창에서 열림)

이 문서에서 사용한 틀: