Phonegap-----Media

当前位置 : 首页 > 网页制作 > CSS > Phonegap-----Media

Phonegap-----Media

来源: 作者: 时间:2016-01-29 09:12
Everything in the code:[html]!DOCTYPE htmlhtmlheadtitleMedia Example/titlescript type=text/javascript charset=utf-8 src=cordova.js/scriptscript type=text/javascript...

Everything in the code:


[]
<!DOCTYPE html> 
<html> 
<head> 
<title>Media Example</title> 
<script type="text/javascript" charset="utf-8" src="cordova.js"></script> 
<script type="text/javascript" charset="utf-8"> 
 
    var my_media = null; 
    var mediaTimer = null; 
    var music_src = "/android_asset/www/music.mp3"; 
     
    document.addEventListener("deviceready",onDeviceReady,false); 
     
    function onDeviceReady(){ 
        playAudio(music_src); 
    } 
 
    function playAudio(src){ 
        my_media = new Media(src,onSuccess,onError); 
        my_media.play(); 
         
        if(mediaTimer == null) 
        { 
            mediaTimer = setInterval(function() 
            { 
                my_media.getCurrentPosition( 
                   function(position){ 
                       if(position > -1) 
                       { 
                           setAudioPosition((position) + " sec"); 
                       } 
                   }, 
                   // error callback 
                   function(e){ 
                       console.log("Error getting pos=" + e); 
                       setAudioPosition("Error: " + e); 
                   } 
               ); 
            }, 
            1000); 
        } 
    } 
     
    function pauseAudio() 
    { 
        if(my_media){ 
            my_media.pause(); 
        } 
    } 
     
    function continueAudio() 
    { 
        if(my_media){ 
            my_media.play() 
        } 
    } 
     
    function stopAudio(){ 
        clearInterval(mediaTimer); 
        mediaTimer = null; 
         
        if(my_media){ 
            my_media.stop(); 
             
            // on an device,it is important to release the media 
            media.release(); 
        } 
    } 
     
    function onSuccess(){ 
        console.log("playAudio():Audio Success"); 
    } 
     
    function onError(error){ 
        alert('code: ' + error.code    + '\n' + 
              'message: ' + error.message + '\n'); 
    } 
     
    function setAudioPosition(position){ 
        document.getElementById('audio_position').innerHTML = position; 
    } 
</script> 
</head> 
<body> 
    <a href="#" onClick="playAudio(music_src);">Play Audio</a> 
    <a href="#" onClick="pauseAudio();">Pause Playing Audio</a> 
    <a href="#" onClick="continueAudio();">continue Playing Audio</a> 
    <a href="#" onClick="stopAudio();">Stop Playing Audio</a> 
    <p id="audio_position"></p> 
</body> 
</html> 

<!DOCTYPE html>
<html>
<head>
<title>Media Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

    var my_media = null;
 var mediaTimer = null;
 var music_src = "/android_asset/www/music.mp3";
 
    document.addEventListener("deviceready",onDeviceReady,false);
 
 function onDeviceReady(){
  playAudio(music_src);
 }

 function playAudio(src){
  my_media = new Media(src,onSuccess,onError);
  my_media.play();
  
  if(mediaTimer == null)
  {
   mediaTimer = setInterval(function()
   {
    my_media.getCurrentPosition(
       function(position){
        if(position > -1)
        {
         setAudioPosition((position) + " sec");
        }
       },
       // error callback
       function(e){
        console.log("Error getting pos=" + e);
        setAudioPosition("Error: " + e);
       }
      );
   },
   1000);
  }
 }
 
 function pauseAudio()
 {
  if(my_media){
   my_media.pause();
  }
 }
 
 function continueAudio()
 {
  if(my_media){
   my_media.play()
  }
 }
 
 function stopAudio(){
  clearInterval(mediaTimer);
  mediaTimer = null;
  
  if(my_media){
   my_media.stop();
   
   // on an Android device,it is important to release the media
   media.release();
  }
 }
 
 function onSuccess(){
  console.log("playAudio():Audio Success");
 }
 
 function onError(error){
  alert('code: ' + error.code    + '\n' +
        'message: ' + error.message + '\n');
 }
 
 function setAudioPosition(position){
  document.getElementById('audio_position').innerHTML = position;
 }
</script>
</head>
<body>
    <a href="#" onClick="playAudio(music_src);">Play Audio</a>
    <a href="#" onClick="pauseAudio();">Pause Playing Audio</a>
    <a href="#" onClick="continueAudio();">continue Playing Audio</a>
    <a href="#" onClick="stopAudio();">Stop Playing Audio</a>
    <p id="audio_position"></p>
</body>
</html>

Comments welcome!!!!!!

Tag:

相关文章

网友评论

<