MediaPlayer pausing and playing error

2019-08-06 19:53发布

问题:

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" />

回答1:

That because I think you have declared and instantiated mp on the onCreate method and it is not accessible in klikopplaybutton() method.

Try something like this.

public class Level01vraag01 extends Activity implements OnClickListener {

    int playknopvariable = 0;
    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vraaglayout);
        setupButtonClickListeners();
        mp = MediaPlayer.create(Level01vraag01.this, R.raw.mcdonalds);

    }

private void setupButtonClickListeners()
{
    ImageView playsoundButton = (ImageView) findViewById(R.id.playsoundbutton);
    playsoundButton.setOnClickListener(this);
}

UPDATE for ERRORS on your ImageView.

You are getting an error in your playsoundButton ImageView object because you again declared it and executed the findViewById in setupButtonClickListeners method and it is not acessible in klikopplaybutton method.

Try this one.

public class Level01vraag01 extends Activity implements OnClickListener {

    int playknopvariable = 0;
    MediaPlayer mp;
    ImageView playsoundButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vraaglayout);
        setupButtonClickListeners();
        mp = MediaPlayer.create(Level01vraag01.this, R.raw.mcdonalds);

    }

    private void setupButtonClickListeners(){
        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;
   }

}