Heim > Web-Frontend > H5-Tutorial > API-Nutzungsanleitung für Medien (Player) in HTML5_HTML5-Tutorial-Fähigkeiten

API-Nutzungsanleitung für Medien (Player) in HTML5_HTML5-Tutorial-Fähigkeiten

WBOY
Freigeben: 2016-05-16 15:46:56
Original
1833 Leute haben es durchsucht

Ich stelle Ihnen nur den Beispielcode zur Verfügung, damit ich nicht zu viel Unsinn mache.


Code kopieren
Der Code lautet wie folgt:





HTML-Audio-API





HTML5 Audio API


HTML5 Audio API-Demo von http://github.com/LearnShare" target="_blank">LearnShare.



Letzte Aktualisierung @2013-04-23 20:40:00
Infotabelle hinzufügen
Update @2013-04-22 14:54:00
DOM-Ereignisse hinzufügen
Update @2013-04-22 12:47:00
getCurrentSrc-Schaltfläche hinzufügen



Code anzeigen auf http ://github.com/LearnShare/LearnShare.github.io/tree/master/labs/audio/" target="_blank">LearnShare.github.io.
API-Referenz: https://developer.mozilla.org/ zh-CN/docs/DOM/HTMLMediaElement" target="_blank">HTMLMediaElement und http://www.w3schools.com/tags/ref_av_dom.asp " target="_blank">Audio/Video DOM References





< ;h2>Audio Element

Öffnen Sie das Entwicklertool um Konsolenprotokolle anzuzeigen.




Controls













< ;button id="play_music1">playMusic1









< ;button id="get_current_time">getCurrentTime
























< ;button id="get_playback_rate">getPlaybackRate










audio-controls.js


复制代码
代码如下:

window.onload=function(){
// autio element abrufen
var audio=document.getElementById("audio");
// play()
document. getElementById("play").onclick=function(){
audio.play();
console.log("play");
};
// pause()
document.getElementById("pause").onclick=function(){
audio.pause();
console.log("pause");
};
// pausiert werden
document.getElementById("get_paused").onclick=function(){
console.log("audio.paused: " audio.paused);
};
// beendet
Dokument .getElementById("get_ended").onclick=function(){
console.log("audio.ended: " audio.ended);
};
// Lautstärke festlegen-
Dokument. getElementById("volume_down").onclick=function(){
audio.volume-=0.2;
console.log("volume-0.2");
};
// Lautstärke einstellen
document.getElementById("volume_up").onclick=function(){
audio.volume =0.2;
console.log("volume 0.2");
};
// get volume
document.getElementById("get_volume").onclick=function(){
console.log("audio.volume: " audio.volume);
};
// get src
document.getElementById("get_src").onclick=function(){
console.log("audio.src: " audio.src);
};
// set src_music1
document.getElementById("play_music1").onclick=function(){
audio.src="./media/music1.mp3";
updateSrc();
updateCurrentSrc();
Konsole .log("play music1");
};
// set src_music2
document.getElementById("play_music2").onclick=function(){
audio.src="./media /music2.mp3";
updateSrc();
updateCurrentSrc();
console.log("play music2");
};
// set Remove_music
Dokument. getElementById("remove_music").onclick=function(){
audio.src="";
console.log("remove music");
};
// get currentSrc
document.getElementById("get_current_src").onclick=function(){
console.log("audio.currentSrc: " audio.currentSrc);
};
// get initialTime
document .getElementById("get_initial_time").onclick=function(){
console.log("audio.initialTime: " audio.initialTime);
};
// Dauer abrufen
document.getElementById ("get_duration").onclick=function(){
console.log("audio.duration: " audio.duration);
};
// Suche abrufen
document.getElementById(" get_seeking").onclick=function(){
console.log("audio.seeking: " audio.seeking);
};
// set currentTime
document.getElementById("jump_to" ).onclick=function(){
audio.currentTime=30;
console.log("jumpTo 30s");
};
// aktuelle Zeit abrufen
document.getElementById(" get_current_time").onclick=function(){
console.log("audio.currentTime: " audio.currentTime);
};
// abgespielt werden
document.getElementById("get_played" ).onclick=function(){
console.log("audio.played:");
var ranges=audio.played;
var n=ranges.length;
for(var i =0;i console.log("(" ranges.start(i) "," ranges.end(i) ")");
}
};
// Autoplay ein
document.getElementById("autoplay_on").onclick=function(){
audio.autoplay=true;
updateAutoplay();
console.log("autoplay on ");
};
// Autoplay aus
document.getElementById("autoplay_off").onclick=function(){
audio.autoplay=false;
updateAutoplay();
console.log("autoplay off");
};
// Autoplay abrufen
document.getElementById("get_autoplay").onclick=function(){
console.log(" audio.autoplay: " audio.autoplay);
};
// Kontrollen zeigen
document.getElementById("controls_show").onclick=function(){
audio.controls=true;
updateControls();
console.log("controls show");
};
// Steuerelemente ausblenden
document.getElementById("controls_hide").onclick=function(){
audio.controls=false;
updateControls();
console.log("controls hide");
};
// Steuerelemente abrufen
document.getElementById("get_controls" ).onclick=function(){
console.log("audio.controls: " audio.controls);
};
// Schleife weiter
document.getElementById("loop_on"). onclick=function(){
audio.loop=true;
updateLoop();
console.log("loop on");
};
// Schleife aus
document.getElementById("loop_off").onclick=function(){
audio.loop=false;
updateLoop();
console.log("loop off");
};
// Schleife abrufen
document.getElementById("get_loop").onclick=function(){
console.log("audio.loop: " audio.loop);
};
// Metadaten vorladen
document.getElementById("preload_metadata").onclick=function(){
audio.preload="metadata";
updatePreload();
console.log("preload metadata ");
};
// Preload abrufen
document.getElementById("get_preload").onclick=function(){
console.log("audio.preload: " audio.preload) ;
};
// get defaultMuted
document.getElementById("get_default_muted").onclick=function(){
console.log("audio.defaultMuted: " audio.defaultMuted);
};
// stummschalten
document.getElementById("mute").onclick=function(){
audio.muted=true;
updateMuted();
console.log("audio mute ");
};
// Stummschaltung aufheben
document.getElementById("unmute").onclick=function(){
audio.muted=false;
updateMuted();
console.log("audio unmute");
};
// stummgeschaltet werden
document.getElementById("get_muted").onclick=function(){
console.log("audio .muted: " audio.muted);
};
// get defaultPlaybackRate
document.getElementById("get_default_playback_rate").onclick=function(){
console.log("audio.defaultPlaybackRate : " audio.defaultPlaybackRate);
};
// set playbackRate-
document.getElementById("playback_rate_down").onclick=function(){
audio.playbackRate-=0.2;
console.log("playbackRate-0.2");
};
// Wiedergaberate festlegen
document.getElementById("playback_rate_up").onclick=function(){
audio.playbackRate =0.2 ;
console.log("playbackRate 0.2");
};
// Wiedergaberate abrufen
document.getElementById("get_playback_rate").onclick=function(){
console.log ("audio.playbackRate: " audio.playbackRate);
};
// get networkState
document.getElementById("get_network_state").onclick=function(){
console.log(" audio.networkState: " audio.networkState);
};
// get readyState
document.getElementById("get_ready_state").onclick=function(){
console.log("audio. readyState: " audio.readyState);
};
// gepuffert werden
document.getElementById("get_buffered").onclick=function(){
console.log("audio.buffered: ");
var ranges=audio.buffered;
var n=ranges.length;
for(var i=0;i console.log("(" ranges.start(i) "," ranges.end(i) ")");
}
};
// suchbar werden
document.getElementById("get_seekable").onclick= function(){
console.log("audio.seekable:");
var ranges=audio.seekable;
var n=ranges.length;
for(var i=0;i< ;n;i ){
console.log("(" ranges.start(i) "," ranges.end(i) ")");
}
};

// DOM-Ereignisse

// abort
audio.addEventListener("abort",function(){
console.log("event:abort");
});
// canplay
audio.addEventListener("canplay",function(){
console.log("event:canplay");
});
// canplaythrough
Audio .addEventListener("canplaythrough",function(){
console.log("event:canplaythrough");
});
// durationchange
audio.addEventListener("durationchange",function( ){
updateDuration();
console.log("event:durationchange");
});
// geleert
audio.addEventListener("emptied",function(){
updateSrc();
updateCurrentSrc();
updateDuration();
updatePaused();
updateNetworkState();
updateReadyState();
updateBuffered();
updateSeekable();
updatePlayed();
console.log("event:emptied");
});
// beendet
audio.addEventListener("ended", function(){
updateEnded();
console.log("event:ended");
});
// geladene Daten
audio.addEventListener("loadeddata",function( ){
updateNetworkState();
updateReadyState();
updateBuffered();
updateSeekable();
console.log("event:loadeddata");
});
// geladene Metadaten
audio.addEventListener("loadedmetadata",function(){
console.log("event:loadedmetadata");
});
// Loadstart
audio.addEventListener("loadstart",function(){
console.log("event:loadstart");
});
// pause
audio.addEventListener("pause",function (){
updatePaused();
console.log("event:pause");
});
// abspielen
audio.addEventListener("play",function() {
updatePaused();
console.log("event:play");
});
// abgespielt
audio.addEventListener("playing",function(){
console.log("event:playing");
});
// progress
audio.addEventListener("progress",function(){
updateNetworkState();
updateReadyState();
updateBuffered();
updateSeekable();
console.log("event:progress");
});
// ratechange
audio.addEventListener ("ratechange",function(){
updatePlaybackRate();
console.log("event:ratechange");
});
// suched
audio.addEventListener(" suchte",function(){
console.log("event:seeked");
});
// suche
audio.addEventListener("seeking",function(){
console.log("event:seeking");
});
// stalled
audio.addEventListener("stalled",function(){
console.log("event:stalled ");
});
// suspend
audio.addEventListener("suspend",function(){
console.log("event:suspend");
});
// timeupdate
audio.addEventListener("timeupdate",function(){
updateCurrentTime();
updateEnded( );
updatePlayed();
console.log("event:timeupdate");
});
// volumechange
audio.addEventListener("volumechange",function(){
updateVolume();
console.log("event:volumechange");
});
// en attente
audio.addEventListener("waiting",function(){
console.log("event:waiting");
});
updateAutoplay();
updateControls();
updateDefaultMuted();
updateDefaultPlaybackRate();
updateLoop ();
updatePreload();
updateSrc();
updateCurrentSrc();
updateDuration();
updateCurrentTime();
updateVolume();
updatePaused( );
updateMuted();
updateEnded();
updatePlaybackRate();
updateNetworkState();
updateReadyState();
updateBuffered();
updateSeekable() ;
updatePlayed();
updateError();
};
// fonctions pour mettre à jour la table d'informations
// autoplay
fonction updateAutoplay(){
document.getElementById ("autoplay").innerHTML=audio.autoplay;
}
//controls
function updateControls(){
document.getElementById("controls").innerHTML=audio.controls;
}
// defaultMuted
function updateDefaultMuted(){
document.getElementById("default_muted").innerHTML=audio.defaultMuted;
}
// defaultPlaybackRate
function updateDefaultPlaybackRate( ){
document.getElementById("default_playback_rate").innerHTML=audio.defaultPlaybackRate;
}
// loop
function updateLoop(){
document.getElementById("loop"). innerHTML=audio.loop;
}
// preload
function updatePreload(){
document.getElementById("preload").innerHTML=audio.preload;
}
/ / src
fonction updateSrc(){
document.getElementById("src").innerHTML=audio.src;
}
// currentSrc
fonction updateCurrentSrc(){
document .getElementById("current_src").innerHTML=audio.currentSrc;
}
// durée
function updateDuration(){
document.getElementById("duration").innerHTML=audio.duration;
}
// currentTime
fonction updateCurrentTime(){
document.getElementById("current_time").innerHTML=audio.currentTime;
}
// terminé
fonction updateEnded(){
document.getElementById("ended").innerHTML=audio.ended;
}
// paused
function updatePaused(){
document.getElementById("paused" ).innerHTML=audio.paused;
}
// muted
function updateMuted(){
document.getElementById("muted").innerHTML=audio.muted;
}
// volume
fonction updateVolume(){
document.getElementById("volume").innerHTML=audio.volume;
}
// playbackRate
fonction updatePlaybackRate(){
document.getElementById("playback_rate").innerHTML=audio.playbackRate;
}
// networkState
function updateNetworkState(){
document.getElementById("network_state").innerHTML=audio. networkState;
}
// readyState
function updateReadyState(){
document.getElementById("ready_state").innerHTML=audio.readyState;
}
// buffered
function updateBuffered(){
var ranges=audio.buffered;
var str="";
var n=ranges.length;
for(var i=0;i str ="(" ranges.start(i) "," ranges.end(i) ")";
if(i!=n-1){
str ="
";
}
}
document.getElementById("buffered").innerHTML=str;
}
// seekable
function updateSeekable(){
plages de var =audio.seekable;
var str="";
var n=ranges.length;
for(var i=0;i str ="(" plages .start(i) "," ranges.end(i) ")";
if(i!=n-1){
str ="
";
}
}
document.getElementById("seekable").innerHTML=str;
}
//played
function updatePlayed(){
var ranges=audio.played;
var str= "";
var n=ranges.length;
for(var i=0;i str ="(" ranges.start(i) "," ranges.end (i) ")";
if(i!=n-1){
str ="
";
}
}
document.getElementById("played") .innerHTML=str;
}
// error
function updateError(){
document.getElementById("error").innerHTML=audio.error;
}
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage