MediaPlayer暂停和播放错误

时间:2013-03-10 20:15:08

标签: java android xml if-statement media-player

我正在使用Java和XML构建Android应用程序。应用程序的用户必须能够收听曲调并暂停。所以我制作了一个按钮,您可以用它来播放并暂停曲调。我做了一个函数,其中必须更改src的{​​{1}}。但是Eclipse在ImageViewmp.start()给出了一个错误,其中说:“mp无法解析”

我不知道我现在做错了什么,我希望有人可以帮助我。

Java代码:

mp.pause()

XML代码:

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;
        }

1 个答案:

答案 0 :(得分:3)

那是因为我认为你已经在onCreate方法上声明并实例化了mp,并且在klikopplaybutton()方法中无法访问它。

尝试这样的事情。

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);
}
对于ImageView上的错误,

更新

你的playsoundButton ImageView对象出现错误,因为你再次声明它并在setupButtonClickListeners方法中执行了findViewById,并且它在klikopplaybutton方法中是不可访问的。

试试这个。

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;
   }

}