Jump to content

[JS] Forcing to stop all audio from playing issue

Recommended Posts

I'm a newbie when it comes to JS but I'm trying to understand it and alter a few snippets to learn more. Here's my issue. I have the JS script running at my page, it autoloads an mp3 and plays it while the page starts. Nothing wrong, everything works. I also have a few buttons on the page that start a new mp3, that also works great. The issue is that it still plays the older mp3 files when selected a new one. Can somebody help me out here? I'm trying to stop all playing audio (no matter howmany buttons are pressed for playing audio) automaticly when a new button (play a new mp3) is pressed, so far no luck. Some help is highly apriciated. The code:

		<script type="text/javascript" charset="utf-8">		// Wait for PhoneGap to load		//		document.addEventListener("deviceready", onDeviceReady, false);		// PhoneGap is ready		//		function onDeviceReady() {			playAudio("/zzz/mypath/to/start.mp3");		}		// Audio player		//		var my_media = null;		var mediaTimer = null;		// Play audio		//		function playAudio(src) {			// Create Media object from src			my_media = new Media(src, onSuccess, onError);			// Play audio			my_media.play();			// Update media position every second			mediaTimer = setInterval(function() {				// get media position				my_media.getCurrentPosition(					// success callback					function(position) {						if (position > -1) {							setAudioPosition(position + " sec");						}					},					// error callback					function(e) {						console.log("Error getting pos=" + e);					}				);			}, 1000);			// SeekTo to 10 seconds after 5 seconds			setTimeout(function() {				my_media.seekTo(10000);			}, 5000);		}		// Stop audio		//		function stopAudio() {			if (my_media) {				my_media.stop();			}			clearInterval(mediaTimer);			mediaTimer = null;		}		// onSuccess Callback		//		function onSuccess() {			console.log("playAudio():Audio Success");		}		// onError Callback		//		function onError(error) {			alert('code: '	+ error.code	+ '\n' +				  'message: ' + error.message + '\n');		}		// Set audio position		//		function setAudioPosition(position) {			document.getElementById('audio_position').innerHTML = position;		}		</script>

an somebody give me a helping hand here?

Edited by SiteSniffer
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...