I'm building an application for Android with Java and XML. The user of the app must be able to listen to a tune and also pause it. So I made one button with which you can play and pause the tune. I made a function in which the src
of the ImageView
must be changed. However Eclipse gives me an error at the mp.start()
and the mp.pause()
, which says: "mp cannot be resolved".
I don't know what I'm doing wrong right now and I hope somebody can help me.
Java code:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class Level01vraag01 extends Activity implements OnClickListener {
int playknopvariable = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vraaglayout);
setupButtonClickListeners();
final MediaPlayer mp = MediaPlayer.create(Level01vraag01.this, R.raw.mcdonalds);
}
private void setupButtonClickListeners() {
ImageView playsoundButton = (ImageView) findViewById(R.id.playsoundbutton);
playsoundButton.setOnClickListener(this);
}
private void klikopplaybutton() {
if (playknopvariable == 0) {
mp.start();
playknopvariable = 1;
playsoundButton.setImageResource(R.drawable.playsoundbutton);
} else {
mp.pause();
playknopvariable = 0;
playsoundButton.setImageResource(R.drawable.pause);
}
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.playsoundbutton:
klikopplaybutton();
break;
}
XML code:
<ImageView
android:id="@+id/playsoundbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/guessbutton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="33dp"
android:src="@drawable/playsoundbutton" />