Software Archive
Read-only legacy content
17061 Discussions

How to use cordova media plugin in intel xdk

Bigfanx__
Beginner
355 Views

Hi, I was trying the cordova media plugin to play sound on intel xdk and android phone. The following code I used works fine on desktop browsers including firefox, ie and chrome and the Intel App Preview installed on my android phone. However, it doesn't work on intel xdk emulator and android phone. I also added the Media plugin in Project configuration., but it won't change anything.  What I am missing in the code? Thanks a lot in advance.

<!DOCTYPE html>
<html>
	<head>
		<title>Blank Hybrid App Project Template</title>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8">

		<!-- <meta name="viewport" content="width=device-width, initial-scale=1"> -->
		<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
		<!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes, minimum-scale=1, maximum-scale=2"> -->

		<style>
			/* see http://dev.w3.org/csswg/css-device-adapt/ */
			@-ms-viewport { width: 100vw ; zoom: 100% ; }                           
			@viewport { width: 100vw ; zoom: 100% ; }
			@-ms-viewport { user-zoom: fixed ; }                                    
			@viewport { user-zoom: fixed ; }
		</style>
		<script>
			// only work on desktop browsers rather than on android
			function playAudio(src) {
			// HTML5 Audio
				if (typeof Audio != "undefined") { 
					new Audio(src).play() ;

				// Phonegap media
				} else if (typeof device != "undefined") {

					// Android needs the search path explicitly specified
					if (device.platform == 'Android') {
						src = '/android_asset/www/' + src;
					}

					var mediaRes = new Media(src,
						function onSuccess() {
						   // release the media resource once finished playing
						   mediaRes.release();
						},
						function onError(e){
							console.log("error playing sound: " + JSON.stringify(e));
						});
					 mediaRes.play();
			   } else {
				   console.log("no sound API to play: " + src);
			   }
		}
		</script>
	</head>

	<body>
		<!-- Main stage for rendering game scenes -->
		<button onclick="playAudio('asset/sound.mp3')">Play</button>
		<script src="intelxdk.js"></script>
		<!-- phantom library, needed for Intel XDK api calls -->
		<script src="cordova.js"></script>
		<!-- phantom library, needed for Cordova api calls -->
		<script src="xhr.js"></script>
		<!-- phantom library, needed for Intel XDK CORS -->

		<script src="js/cordova-init.js"></script>
		<script src="js/app.js"></script>
		
	</body>

</html>

 

0 Kudos
4 Replies
PaulF_IntelCorp
Employee
355 Views
0 Kudos
Anusha_M_Intel1
Employee
355 Views

You can refer to Paul's link on how to access the sound file within the android file system. As for Intel XDK emulator, it does not support .mp3 files. Only .wav and .ogg files. Hope that helps.

0 Kudos
Bigfanx__
Beginner
355 Views

Thanks lot for help. I'll test the above example asap.

0 Kudos
Bigfanx__
Beginner
356 Views

Hi, I tested the cordova media plugin by following the above example. It works like charm on android 4.4 and the xdk emulator. Thanks a lot :-)

0 Kudos
Reply