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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值