• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Where to place mp3 files?

GIR

Member
Hello All,

I am trying to develop a simple soundboard app for my son to play with.

I am using developing on Ubuntu 10.04 LTS, using Eclipse SDK Version: 3.5.2, Build id: M20100211-1343.

When a new project is setup for 2.1 API, in which directory do I place the mp3 file?

I have spent a lot of time searching this and other forums, as well as google, but I seem to find only examples and details for 1.5 API.

Many thanks,
GIR
 
OK,

This turned out to be a simple fix!

I had to manualy create the new 'folder' named raw in the res directory.

While this was a simple matter to correct it took around 4 hours to find the correct information on the internet because many of the tutorials are aimed at the 1.5 API.

Code:
package com.nds.ZIM;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ZIM extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final MediaPlayer cow = MediaPlayer.create(this, R.raw.bring_me_coww);
        final MediaPlayer dance = MediaPlayer.create(this, R.raw.dancewithg);
        final MediaPlayer doodee = MediaPlayer.create(this, R.raw.doodee_attack);
        final MediaPlayer doom = MediaPlayer.create(this, R.raw.doomsong);
        final MediaPlayer running = MediaPlayer.create(this, R.raw.girrunning);
        final MediaPlayer mission = MediaPlayer.create(this, R.raw.girs_mission_statement);
        


        
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);
        Button button4 = (Button)findViewById(R.id.button4);
        Button button5 = (Button)findViewById(R.id.button5);
        Button button6 = (Button)findViewById(R.id.button6);
        
        button1.setOnClickListener(new OnClickListener() {
            @Override
             public void onClick(View v) {
                // Do stuff here
                

                try {
                    cow.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                cow.start();
            }}
            );
        button2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // Do stuff here
                try {
                    dance.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                dance.start();
                
           }});
        button3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // Do stuff here
                try {
                    doodee.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                doodee.start();
                
           }});
        button4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // Do stuff here
                try {
                    doom.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                doom.start();
                
           }});
        button5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // Do stuff here
                try {
                    running.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                running.start();
                
           }});
        button6.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // Do stuff here
                try {
                    mission.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mission.start();
                
           }});
        
        
        
  
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
    }
}

Is there a neater way to arrange the button listerners and their setup declaration?

Any help would be appreciated.
GIR
 
you can create one listener , and have 'em all go to the same listener
inside the listener , you determine which button is clicked and start the correct mediaplayer you like (using if statements or switch , your call)
in mine , I did it like this inside the onclick
Code:
switch (v.getId()) {
            case R.id.button1:
                sounder(1);
                break;

            case R.id.button2:
                sounder(2);
                break;
//..etc
}
where sounder is another function that plays the proper sound (I actually used one media player and changed it's source, but it didn't turn out to be that good of an idea .. used to crash when pressing on the same button too fast [probably form that] try your method and tell me :D)

Edit: I assumed you know Java well .. but maybe I assumed wrong
so here's how to do it [doing this to avoid continue studying :P]

first , create a listener for yourself
Code:
View.OnClickListener listen = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
//your switch statement goes here
}
and add the proper switch statement thingie

then for each button , do this
Code:
button1.setOnClickListener(listen);
button2.setOnClickListener(listen);
//..etc
 
  • Like
Reactions: GIR
Many thanks JiMMaR,

I completed the app, installed it onto my ZTE Racer, and it works great!

I tried to crash it by pressing all the buttons in quick succession, but the worst that happens is that multiple sounds play at the same time. It does not repeat the same sound many times, which is excellent.

Today I shall experiment with the code samples you provided for firing the events of multiple buttons.

That was a great reply and very helpful, so in addition to merely clicking on the 'Thanks' button, I'd like to thank you for the time and effort you spent answering my question.

All the best,
GIR
 
np , glad my knowledge of sound boards actually helped someone :D
now you release it on the market :p
 
np , glad my knowledge of sound boards actually helped someone :D
now you release it on the market :p

That would be the next step, but I think there are too many soundboards on the market already (just my humble view :) ).

I have just completed registering a google developer account, so I do aim to get a paid app and an ad supported free version onto the market soon.
 
Back
Top