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 Eclipse
the raw
folder is not created by default, you have to create it: right-click on the res folder, select Novo...
after Pasta
and enter the folder name, as in the image:
Then you can create new files there, either by dragging and dropping the file into it, or right-click the raw folder and select% with% and then% with%.