Error trying to use Mediaplayer

Asked

Viewed 113 times

0

I tried to elaborate a Button that when pressed performs a sound, but I know how to import that sound and I don’t know how to locate the Raw folder, when I try to use that line:

mpSongButtons = MediaPlayer.create(MainActivity.this, R.raw.);

From the mistake, that is, this Raw does not exist, someone could help me?

1 answer

2


public class ExecutaSomExemplo {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Conecta o button do xml
        Button button = (Button)findViewById(R.id.myButton);

        //Método de ação do clique
        button.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {

                      //Cria o objeto MediaPlayer que irá receber o arquivo de som
                      MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.nomedoarquivo);

                       //Toca a música
                       mPlayer.start();
                    }
                }
        );
    } 
}

In your specific case where you are using the Eclipse the briefcase raw does not come created as default, you have to create it: right click on the res folder, select Novo... afterward Pasta and type the folder name as in the image:

nova pasta

So, you can create new files there, either by dragging-and-dropping the file inside it, or right-click again on the raw folder and select Novo... and then Arquivo.

  • but I cannot use this method because there is no R.raw.filename address, error when I try to use "raw cannot be resolved or is not a field"

  • Make sure the file is of the type .mp3 example: filename.mp3, also make sure the file is in the directory res/raw of your project. If everything is right, give a Clean/Build and try to rotate again.

  • But there is no Raw folder, that is my doubt, there is no such folder in my project, I am using the latest version of ADT Plugin

  • Which IDE are you using @Jeiferson? Eclipse, Netbeans, Android Studio?

  • IDE Eclipse @iTSangar

  • I added a brief solution to your case in the answer I created @Jeiferson, in case you need anything else just say =)

  • Vlw man, that was it...

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.