Discussion Forums  >  Audio, Video, Playing, Recording

Replies: 9    Views: 161

pimentazores
Aspiring developer
Profile
Posts: 47
Reg: Mar 26, 2013
Barcelona
8,020
10/12/13 04:54 PM (10 years ago)

Sounds for android

Hi guys!, Is there any simple way of playing sounds when buttons are pressed for android? For example, menu buttons, each button playing a sound? I have tried (I think) everything I've found around here at the forums but simply not working... I am not self hosted. Thanks in advance Pedro
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
10/25/13 03:31 AM (10 years ago)
The answer is 'yes and no'. 'Simple' is a relative term. What is simple for some is difficult for others, but yes it can be done :) It won't matter if you're self hosted or not. You will need to modify the code in order for this to happen no matter if you're Self or BT hosted. This is where I used code I stole from the Quiz plugin to play audio in my HTML plugin. https://www.buzztouch.com/forum/thread.php?tid=0DAA7A1F05FA0EA2DB0F277 You can pretty much do the same thing, just be sure to get it all placed correctly: 1) declare your variables. 2) Initiate your media player in the 'onCreate' method 3) Implement your media player in whatever button press code you want. Cheers! -- Smug
 
pimentazores
Aspiring developer
Profile
Posts: 47
Reg: Mar 26, 2013
Barcelona
8,020
like
10/28/13 04:39 PM (10 years ago)
Hi Smug, thanks for your reply, and sorry for my late feedback - I've been out for a couple of days. I am trying to modify the code in my BT_screen_Htmldoc.java, copying your code and putting it in the right places, but can't get any sound playing... I have noticed that in the BT_config.txt my sound file was referred as "soundEffectFileName"... I don't know if this is important.
 
pimentazores
Aspiring developer
Profile
Posts: 47
Reg: Mar 26, 2013
Barcelona
8,020
like
10/28/13 04:53 PM (10 years ago)
Hi Smug, thanks for your reply, and sorry for my late feedback - I've been out for a couple of days. I am trying to modify the code in my BT_screen_Htmldoc.java, copying your code and putting it in the right places, but can't get any sound playing... I have noticed that in the BT_config.txt my sound file was referred as "soundEffectFileName"... I don't know if this is important.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
10/28/13 05:59 PM (10 years ago)
But if you snoop through the android code, you'll see the background 'sound effects' are not enabled. For the moment, developer intervention is required. Cheers! -- Smug
 
pimentazores
Aspiring developer
Profile
Posts: 47
Reg: Mar 26, 2013
Barcelona
8,020
like
10/29/13 05:22 AM (10 years ago)
Hi Smug! I’m not sure if I’m going in the right direction here or if I’m completely lost… I tried to activate the sound effects changing “audioFileName” for “soundEffectFileName” in the code you’ve suggested. So, this is the final result in my BT_screen_Htmldoc.java (basically, I’ve changed “OLD CODE” for “NEW CODE” one) : I declared 3 new variables: OLD CODE: private String audioFileName = ""; NEW CODE: private String soundEffectFileName= ""; private String pageTitle = ""; private MediaPlayer myMediaPlayer; In my 'onCreate' method I put this code right above the 'setup saveAsFilename' area: OLD CODE: audioFileName = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), "audioFileName", ""); NEW CODE soundEffectFileName = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), " soundEffectFileName ", ""); pageTitle = BT_strings.getJsonPropertyValue(this.screenData.getJsonObject(), "localFileName", ""); OLD CODE: BT_debugger.showIt(activityName + ": Audio File Name: " + audioFileName); NEW CODE: BT_debugger.showIt(activityName + ": Sound Effect File Name: " + soundEffectFileName); BT_debugger.showIt(activityName + ": Page Name: " + pageTitle); ////////////////// if (pageTitle.toString().equals("about.html")) { BT_debugger.showIt(activityName + ": About Audio"); OLD CODE: if(BT_fileManager.doesProjectAssetExist("BT_Audio", audioFileName)){ BT_debugger.showIt(activityName + ": File Exists"); try{ AssetFileDescriptor afd = getAssets().openFd("BT_Audio/" + audioFileName); NEW CODE: if(BT_fileManager.doesProjectAssetExist("BT_Audio", soundEffectFileName)){ BT_debugger.showIt(activityName + ": File Exists"); try{ AssetFileDescriptor afd = getAssets().openFd("BT_Audio/" + soundEffectFileName); myMediaPlayer = new MediaPlayer(); myMediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength()); myMediaPlayer.prepareAsync(); }catch(Exception e){ BT_debugger.showIt(activityName + ":EXCEPTION setting up soundPlayerCorrect. Message: " + e.toString()); } } } /////////////////////////// And, for the rest of the code I didn’t changed anything, just added as you suggested on its right places. I'm code illiterate (I'm learning the basics and hopefully will move to next learning level asap...). I feel like I'm getting closer here, but again, maybe I’m just going in the wrong direction... If that is the case, please let me know and I'll just continue studying & learning before bothering you again! ;) Thanks again for all your help and support! Pedro
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
10/29/13 06:14 AM (10 years ago)
Well, keep in mind that those json values *must* be available to your plugin, whatever it might be. If you look at your BT_screenHTML plugin control panel, you'll notice that the json value for the audio uses the key name of 'audioFileName' which is why I used it. You can use whatever 'variable name' you want, but when you pull information from the BT_config.txt file for that plugin, be sure to use valid key names for the correct values. https://dl.dropboxusercontent.com/u/115208762/bgaudio.png Cheers! -- Smug
 
pimentazores
Aspiring developer
Profile
Posts: 47
Reg: Mar 26, 2013
Barcelona
8,020
like
10/29/13 07:49 AM (10 years ago)
Hi Smug, Got it! (I think) I’ve been doing all wrong from the beginning. I was trying to add my sound file at the “button click” point, at menu buttons plugin, instead of when hmtl doc opens, in html plugin… That is what should trigger the sound to play, right? I realized the mistake when I saw the screenshot you’ve attached on your last reply. Also, if I finally get this to work when my html file opens, it could be possible to adapt the code to use it when a button is pressed (using menu buttons plugin), what do you think? Can´t wait to try it, I’ll let you know the result!! Thanks again&again! :)
 
pimentazores
Aspiring developer
Profile
Posts: 47
Reg: Mar 26, 2013
Barcelona
8,020
like
10/29/13 02:20 PM (10 years ago)
And.... it worked!! :))) You rock! Thanks again!
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
10/29/13 03:27 PM (10 years ago)
It's a LOT more fun when it works :) Glad you're running! Cheers! -- Smug
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.