Using radio buttons to play .ogg

All your problems with Audio, Video and Images.

Using radio buttons to play .ogg

Postby Sport40L » Thu Dec 03, 2009 6:29 pm

Hello,

I'm new to Android development, but have a little Java experience. I just finished designing my UI and I have three radio buttons I would like to use to play a .ogg file based on their selection. There is a separate stop button at the bottom to cease play. What would be the best way to go about implementing this? Should I create a mediaPlayer object and then use a onClick handler to set which file to play? Any help or examples you can provide would be helpful. Thanks in advance.
Sport40L
Freshman
Freshman
 
Posts: 6
Joined: Thu Dec 03, 2009 6:21 pm

Top

Postby appforce » Thu Dec 03, 2009 8:13 pm

Hi,

Your RadioButtons should be nested into RadioGroup and you should use setOnCheckedChangeListener on it. For the playing itself - if your .ogg-s are very small you should use SoundPool, otherwise MediaPlayer is needed, but you have to run the prepare() method in separate thread to prevent blocking the user input. Or you can use prepareAsync with setOnPreparedListener. Make sure you're doing a reset() on the MediaPlayer each time you switch to different .ogg file.


Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby Sport40L » Thu Dec 03, 2009 8:20 pm

appforce wrote:Hi,

Your RadioButtons should be nested into RadioGroup and you should use setOnCheckedChangeListener on it. For the playing itself - if your .ogg-s are very small you should use SoundPool, otherwise MediaPlayer is needed, but you have to run the prepare() method in separate thread to prevent blocking the user input. Or you can use prepareAsync with setOnPreparedListener. Make sure you're doing a reset() on the MediaPlayer each time you switch to different .ogg file.


Android developers


The radio button layout is correctly nested into a RadioGroup. Each .ogg is a little less than half a meg. Is this small enough? There are only three in the entire program, and I need each to loop until stop is pressed. Unless a new sound is chosen, in that case the previous will stop.
Sport40L
Freshman
Freshman
 
Posts: 6
Joined: Thu Dec 03, 2009 6:21 pm

Postby appforce » Thu Dec 03, 2009 8:37 pm

Hi,

I'm not sure if they will fit in SoundPool, but most likely not. To make this sounds best you can create 3 MediaPlayers, setLooping and prepareAsync all of them in onCreate(). They should have OnPreparedListener set, and when all ready set the OnCheckedChangeListener of the RadioGroup (so if user switches the RadioButtons before players are ready nothing happens). The switching can be with pause() and start() so there will be no gap.

Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby Sport40L » Thu Dec 03, 2009 8:41 pm

appforce wrote:Hi,

I'm not sure if they will fit in SoundPool, but most likely not. To make this sounds best you can create 3 MediaPlayers, setLooping and prepareAsync all of them in onCreate(). They should have OnPreparedListener set, and when all ready set the OnCheckedChangeListener of the RadioGroup (so if user switches the RadioButtons before players are ready nothing happens). The switching can be with pause() and start() so there will be no gap.

Android developers


Thank you for the quick reply. Will creating 3 MediaPlayers have much impact on efficiency or performance?
Sport40L
Freshman
Freshman
 
Posts: 6
Joined: Thu Dec 03, 2009 6:21 pm

Postby appforce » Thu Dec 03, 2009 8:46 pm

Hi,

It think not (depends on what else your app needs to do). You can always move to full prepare cycle on RadioButton switch, the gap will be small for <500k. Best optimization also depends on how often is expected to change sounds. Full prepare cycle is light on memory but heavy on CPU and IO, pause/start way is vice versa.

Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Top

Postby Sport40L » Thu Dec 03, 2009 8:51 pm

The switch between sounds doesn't have to be gapless or immediate. So choosing the playing sound and making sure it does not overlap other sounds is the important thing.
Last edited by Sport40L on Fri Dec 04, 2009 5:30 am, edited 1 time in total.
Sport40L
Freshman
Freshman
 
Posts: 6
Joined: Thu Dec 03, 2009 6:21 pm

Postby appforce » Thu Dec 03, 2009 8:58 pm

That means the best way is to use a single player and when button is checked:
1. stop the player - catch IllegalStateException
2. setDataSource according to the checked button
3. setLooping
4. setOnPreparedListener containing only mp.start()
5. prepareAsync



Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby Sport40L » Thu Dec 03, 2009 9:19 pm

appforce wrote:That means the best way is to use a single player and when button is checked:
1. stop the player - catch IllegalStateException
2. setDataSource according to the checked button
3. setLooping
4. setOnPreparedListener containing only mp.start()
5. prepareAsync



Android developers


I have the following, but my program is still crashing

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.   mp.stop();
  3. } catch (IllegalStateException e) {
  4.    }
  5. mp = MediaPlayer.create(getBaseContext(), R.raw.white_noise);
  6. mp.start();
  7. mp.setLooping(true);
  8.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
Sport40L
Freshman
Freshman
 
Posts: 6
Joined: Thu Dec 03, 2009 6:21 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 6 guests