Languages

Menu
Sites
Language
Sounds are not being Played

I have and htmol page in which i have six button . Each button have onVlivk function activated and onClick a sound will be played. Sounds are in mp3 format. Now i when i click on any button sound will be played but if i click on another button in between of sound then the recently clicked button will not polayed sound. Means if there is click in between of sound duration other sound will not be played. Now after this if i click on any button sound will not be played. \

Whats the problem i am unable to understand.
 

Here is my code.
 

    <button class="stage1" style="width: 280px;height: 130px; margin-top: 40px;margin-left: 40px; background: transparent;  "onclick="audio('a')" ></button>

 

 

In Javascript:

 

    function audio(audio_name) {

            audioElement.setAttribute('src', 'audio/' + audio_name + '.mp3');
            
            audioElement.play();
            
        }
   
 

 

Edited by: Brock Boland on 17 Mar, 2014 Reason: Paragraph tags added automatically from tizen_format_fix module.

Responses

7 Replies
Lakshmi Grandhi
Hi, Below code is working fine, let me know if you are facing issues.
audioElement = document.createElement('audio');   
	$("#bt1").on( "vclick", function(){
		     
		audioElement.setAttribute('src', '/opt/media/Sounds/' + '1.mp3');
		audioElement.play();
	}) ;
	$("#bt2").on( "vclick", function(){
		     
		audioElement.setAttribute('src', '/opt/media/Sounds/' + '2.mp3');
		audioElement.play();

	}) ;
	$("#bt3").on( "vclick", function(){
		   
		audioElement.setAttribute('src','/opt/media/Sounds/'+ '3.mp3');
		audioElement.play();

	}) ;
Noureen Akhter
if i use this method then previous sound will not be stop onclick on second sound
Lakshmi Grandhi
Hi noureen, Then you have to create different audio element and play.
Lakshmi Grandhi
try this
var audioElement1 ;
var audioElement2 ;
var audioElement3 ;
var init = function () {
	$('div[data-role="page"]:first .ui-btn-back').bind("click", function(event) {
		var currentApp = tizen.application.getCurrentApplication();
		currentApp.exit();
	});
	// TODO:: Do your initialization job
	console.log("init() called");
	audioElement = document.createElement('audio');   
	$("#bt1").on( "vclick", function(){
		if( !audioElement1)
		 audioElement1   = document.createElement('audio'); 
		audioElement1.setAttribute('src', '/opt/media/Sounds/' + '1.mp3');
		audioElement1.play();
	}) ;
	$("#bt2").on( "vclick", function(){
		if( !audioElement2)
		 audioElement2   = document.createElement('audio'); 
		audioElement2.setAttribute('src', '/opt/media/Sounds/' + '2.mp3');
		audioElement2.play();

	}) ;
	$("#bt3").on( "vclick", function(){
		if( !audioElement3)
		 audioElement3   = document.createElement('audio');    
		audioElement3.setAttribute('src','/opt/media/Sounds/'+ '3.mp3');
		audioElement3.play();

	}) ;
};
$(document).bind('pageinit', init);
Noureen Akhter
why i have to make multiple objects means is it a bad approach. And if i do that the problem would remain the same previous sound will not be stop. And if there is 100 button than i have to make 100 objects for each sound. This problem was not in tizen sdk version 2.0.0a and device version 2.0. Now when i upgrade to tizen sdk and device version 2.1 it is not playing sounds. My project was ready but on upgrading i am unable to proceed
Lakshmi Grandhi
one audio element should point to single audio source, if you are changing the source of audio element in button2 click element then it should play second song, obviously first song will be stopped, since you have changed the source of audio element, if you want first song to continued while second is playing you need to have different audio elements.
Noureen Akhter
I simply did this and its working. What i was looking for was exactly that var audioElement = document.createElement('audio'); function audio(audio_name) { Disable(); audioElement = document.createElement('audio'); audioElement.setAttribute('src', 'audio/' + audio_name + '.ogg'); audioElement.load(); audioElement.play(); } function Disable() { audioElement.duration=0; audioElement.pause(); }