javaandroidonclick

Play several mp3 files in one Onclick statement?


I'm learning Android and my first app is a simple music instrument. I have 7 buttons at the moment, and I'm using MediaPlayer for each one. The thing is that every button has the same functionality, and I want to avoid a switch statement on the Onclick method - which is the only thing I find on the web. Someone told me to name the buttons with the correspondent mp3 file and write a simple "play" function that would match the right key/button, but I guess I'm not doing it right. Can someone help me out? Here's what I've got, and much appreciated for the help:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

   //these buttons have the same name as the mp3 key notes file

    c1= (Button) findViewById(R.id.key1); 
    c1.setOnClickListener(this);
    d1= (Button) findViewById(R.id.key2);
    d1.setOnClickListener(this);
    e1= (Button) findViewById(R.id.key3);
    e1.setOnClickListener(this);
    f1= (Button) findViewById(R.id.key4);
    f1.setOnClickListener(this);        
    g1= (Button) findViewById(R.id.key5);
    g1.setOnClickListener(this);
    a1=(Button) findViewById(R.id.key6);
    a1.setOnClickListener(this);
    b1=(Button) findViewById(R.id.key7);
    b1.setOnClickListener(this);

The OnClick method (the first if example works, of course, but if I want a complex instrument I'll have repeated code. So I want this "else" to be the only statement):

@Override
public void onClick(View v) {

    Button b = (Button)v;
    String note=b.getText().toString();

    if(v==c1){
        mp = MediaPlayer.create(this, R.raw.c1);
        mp.start();
       }
      else{
        play( note+".mp3");
      }

And the attempt of the play method:

  public void play(String note){
  note.equals("cs2");//?? 

  //the mp3 files are stored in res/raw and I also tried that specific path, but it didn't work. The keys are silent, except the c1

 }        

EDIT: Now I have only this:

    public void play(String note){
      mp = MediaPlayer.create(this,getResources().getIdentifier(note,"raw",getPackageName()));
      mp.start();
    }

    @Override
    public void onClick(View v) {

    clickCount++;
    updateClickCount();

    Button b = (Button)v;
    String note=b.getText().toString();
    play(note);
    }

But the app still crashes

EDIT: The XML

            <?xml version="1.0" encoding="utf-8"?>

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.vtorferreira.sounds.MainActivity">



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vtorferreira.sounds.MainActivity">

    <ImageView
        android:layout_width="300dp"
        android:layout_height="1200dp"
        android:id="@+id/kid"
        android:src="@drawable/kid"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:clickable="true"/>



    <ImageView
        android:layout_width="300dp"
        android:layout_height="1200dp"
        android:id="@+id/partido"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:clickable="true"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="10">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:orientation="horizontal"
            android:weightSum="34"
            android:gravity="center">

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#9BC53D"
                android:visibility="invisible"
                android:id="@+id/key1_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black1"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#2660A4"
                android:visibility="invisible"
                android:id="@+id/key2_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black2"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#399E5A"
                android:visibility="invisible"
                android:id="@+id/key3_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#5F0F40"
                android:visibility="invisible"
                android:id="@+id/key4_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black3"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#FA7921"
                android:visibility="invisible"
                android:id="@+id/key5_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black4"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#9BC53D"
                android:visibility="invisible"
                android:id="@+id/key6_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black5"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#2660A4"
                android:visibility="invisible"
                android:id="@+id/key7_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#399E5A"
                android:visibility="invisible"
                android:id="@+id/key8_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black6"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#5F0F40"
                android:visibility="invisible"
                android:id="@+id/key9_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black7"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#FA7921"
                android:visibility="invisible"
                android:id="@+id/key10_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black8"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#9BC53D"
                android:visibility="invisible"
                android:id="@+id/key11_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#2660A4"
                android:visibility="invisible"
                android:id="@+id/key12_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black9"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#399E5A"
                android:visibility="invisible"
                android:id="@+id/key13_ext"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#000000"
                android:visibility="invisible"
                android:id="@+id/black10"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#5F0F40"
                android:visibility="invisible"
                android:id="@+id/key14_ext"/>









        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:orientation="horizontal"
            android:weightSum="7"
            android:gravity="center">

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#2660A4"
                android:visibility="invisible"
                android:id="@+id/key1"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#2660A4"
                android:visibility="invisible"
                android:id="@+id/key2"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#399E5A"
                android:visibility="invisible"
                android:id="@+id/key3"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#5F0F40"
                android:visibility="invisible"
                android:id="@+id/key4"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#FA7921"
                android:visibility="invisible"
                android:id="@+id/key5"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#9BC53D"
                android:visibility="invisible"
                android:id="@+id/key6"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#2660A4"
                android:visibility="invisible"
                android:id="@+id/key7"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#399E5A"
                android:visibility="invisible"
                android:id="@+id/key8"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#5F0F40"
                android:visibility="invisible"
                android:id="@+id/key9"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#FA7921"
                android:visibility="invisible"
                android:id="@+id/key10"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#9BC53D"
                android:visibility="invisible"
                android:id="@+id/key11"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#2660A4"
                android:visibility="invisible"
                android:id="@+id/key12"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#399E5A"
                android:visibility="invisible"
                android:id="@+id/key13"/>

            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:background="#5F0F40"
                android:visibility="invisible"
                android:id="@+id/key14"/>





        </LinearLayout>

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:orientation="horizontal"
            android:weightSum="10"
            android:gravity="center">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:id="@+id/pontos"
                android:textSize="15sp"
                android:textStyle="normal|bold" />

        </LinearLayout>

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal"
            android:weightSum="8"
            android:gravity="center">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal"
                android:gravity="center">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"
                    android:background="#2072CA"
                    android:id="@+id/pref"
                    android:text="Preferences"

                    />
                <ToggleButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"
                    android:background="#2072CA"
                    android:id="@+id/solo"
                    android:textOff="Play Magic Piano!"
                    android:textOn="Close Piano"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"
                    android:background="#2072CA"
                    android:id="@+id/learn"
                    android:text="Learn about piano"
                    />

            </LinearLayout>

        </LinearLayout>


      </LinearLayout>

     </RelativeLayout>



   </RelativeLayout>

Solution

  • Remove file extension (+ ".mp3") and use :
    getResources().getIdentifier("FILE NAME WITHOUT EXTENSION","raw", getPackageName())

    Also make sure each of your button text is holding a filename present in your raw folder without extension. For exemple "c1".

    Here is how should look final code :

    @Override
    public void onClick(View v) {
        Button b = (Button)v;
        String note = b.getText().toString().toLowerCase();
        play(note);
    }
    
    public void play(String note){
      try {
        mp = MediaPlayer.create(this,getResources().getIdentifier(note,"raw", getPackageName()));
        mp.start();
      } catch (Exception e) {
         Log.e("Error", "error playing file with name : " + note + "\n" + e);
      }
    
    }  
    

    You must set text on your buttons elements. For each one, you should display a filename. for example adding android:text"c1" :

    <Button
       android:text="c1"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="0.5"
       android:layout_marginLeft="3dp"
       android:layout_marginRight="3dp"
       android:background="#2660A4"
       android:visibility="visible"
       android:id="@+id/key1"/>
    

    To clarify a bit, the code retrieve the text on the button. for example 'c1'. Then, your app will try to find the file with name c1 in your raw folder. If you don't set any text on your buttons... Then of course, your app won't find any resource with name '' ! Hope you understand what I mean.