Jump to content

[JS] Forcing to stop all audio from playing issue


SiteSniffer
 Share

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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...