Hi,
I'm trying to write a simple app that plays a sound when a button is clicked and then stops playing it when the button is clicked again. I'll just copy-paste the code:
There is a file called play.mp3 in /res/raw. When I try and run it I receive an error saying the app stopped unexpectedly. I've attached a screenshot from the debug window.
Is it something wrong with the code? Or is it the emulator's fault?
Does the play.mp3 file get uploaded on the emulator as well as part of the app?
I'm trying to write a simple app that plays a sound when a button is clicked and then stops playing it when the button is clicked again. I'll just copy-paste the code:
package tutorial.helloviews;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HelloViews extends Activity {
boolean playing = false;
MediaPlayer mp = MediaPlayer.create(this, R.raw.play);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.play);
if(mp != null)
{
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if(playing)
{
mp.start();
playing = true;
}
else
{
mp.stop();
playing = false;
}
}
});}
}
}
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HelloViews extends Activity {
boolean playing = false;
MediaPlayer mp = MediaPlayer.create(this, R.raw.play);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.play);
if(mp != null)
{
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if(playing)
{
mp.start();
playing = true;
}
else
{
mp.stop();
playing = false;
}
}
});}
}
}
There is a file called play.mp3 in /res/raw. When I try and run it I receive an error saying the app stopped unexpectedly. I've attached a screenshot from the debug window.
Is it something wrong with the code? Or is it the emulator's fault?
Does the play.mp3 file get uploaded on the emulator as well as part of the app?