0

I apologize for my noobiness in java, but I am trying to make a very basic app for someone for their birthday and have only really done any programming in python. I have been trying to implement the code found in android - how to make a button click play a sound file every time it been pressed? and am having trouble. I have placed the assets folder in the main directory, the src directory, and the app directory to see if any helped and am still getting the error

Every time I attempt to run the program I get the following error

02-27 23:06:48.896  25643-25643/com.app.bdking.mineturtle W/System.err﹕ java.io.FileNotFoundException: hello.mp3

Here is my main activity

package com.app.bdking.mineturtle;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final MediaPlayer mp = new MediaPlayer();
        Button b = (Button) findViewById(R.id.button1);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(mp.isPlaying())
                {
                    mp.stop();
                }

                try {
                    mp.reset();
                    AssetFileDescriptor afd;
                    afd = getAssets().openFd("hello.mp3");
                    mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                    mp.prepare();
                    mp.start();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }



            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

main xml is the same as on the aforementioned post.

Thanks in advance.

Community
  • 1
  • 1

2 Answers2

1

You need to just Put your file in res/raw folder and use

 public void onClick(View v) {

            if(mp.isPlaying())
            {
                mp.stop();
            } else{

            try {
                mp = MediaPlayer.create(this, R.raw.hello);
                mp.prepare();
                mp.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

}

Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46
  • When I replace this with my onlick it says it cannot cannot resolve the create(this....) method – bailey2092 Feb 28 '15 at 07:27
  • ok, then use "MainActivity.this" insted of "this" if your class name is "MainActivity" – Rahul Sharma Feb 28 '15 at 07:30
  • Hopefully last question, now I am getting an error saying on like "mp = MediaPlayer.create(MainActivity.this, R.raw.hello);" saying it cannot assign a value to that variable – bailey2092 Feb 28 '15 at 07:42
  • Keep your MediaPlayer declaration line outside onCreate() method. i.e. write this line above on create method "MediaPlayer mp = new MediaPlayer();" and also remove final keyword – Rahul Sharma Feb 28 '15 at 08:00
0

You can do it in another way. Put the .mp3 files under res/raw folder and use the following code:

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.android);
 mediaPlayer.start();

Refer this link for better example to understand

Fahim
  • 12,198
  • 5
  • 39
  • 57
  • I apologize. What do I replace this code with? Or is this the only code I need in the file? Like I said, sadly zero java experience, though it's next on the list. – bailey2092 Feb 28 '15 at 06:33
  • Check the updated answer. Also accept the answer if you are satisfied – Fahim Feb 28 '15 at 06:44