Phonegap-----Media

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 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!!!!!!

 

 

posted @ 2013-06-26 19:48  爱生活,爱编程  阅读(250)  评论(0编辑  收藏  举报