3

我在 Eclipse 中创建了一个应用程序来播放和停止 mp3 文件。一切都很好,除了当我播放音频文件并停止它并且我想重播它时,播放 btn 不起作用!我想知道是否有人可以帮助我?提前致谢。代码如下:

package ir.polyglotcenter.childrenmostfrequentwords;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Related to the media player
    final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);
    //Button related to play btn
    Button myButtonOne = (Button) findViewById(R.id.btn_audio);
    myButtonOne.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            myMediaPlayer.start();

        }
    });

    //Button related to stop btn
    Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
    myButtonTwo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            myMediaPlayer.stop();

        }
    });

}
}
4

4 回答 4

1

如果您想在 Button Click 上开始/停止声音,然后使用MediaPlayer.start()MediaPlayer.pause()暂停当前播放的声音,该声音在开始按钮 click 上再次开始。并覆盖 ActivityonPause以最终停止并释放 MediaPlayer。将您的代码更改为:

onStop 按钮单击:

myButtonTwo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            myMediaPlayer.pause();  //>>pause current sound
            myMediaPlayer.seekTo(0); //>> seek to start it again 

        }
    });

并覆盖 Activity 的 onPause 方法:

  @Override
   protected void onPause{
      super.onPause();
       myMediaPlayer.stop();  //>>> stop myMediaPlayer
       myMediaPlayer.release(); //>>> free myMediaPlayer
     }
于 2013-03-11T19:06:39.133 回答
0

试试这个(只需在公共区域定义媒体播放器)

    public class Main extends Activity {

MediaPlayer myMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Related to the media player

    myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);

    //Button related to play btn
    Button myButtonOne = (Button) findViewById(R.id.btn_audio);
    myButtonOne.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            myMediaPlayer.start();

        }
    });

    //Button related to stop btn
    Button myButtonTwo = (Button) findViewById(R.id.btn_stop);
    myButtonTwo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            myMediaPlayer.pause();

        }
    });

}
}
于 2013-03-11T19:02:56.670 回答
0

您必须移动此代码:

final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this, R.raw.audio);

在 :

myButtonOne.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        final MediaPlayer myMediaPlayer = MediaPlayer.create(Main.this,R.raw.audio);
        myMediaPlayer.start();

    }
});
于 2017-02-11T12:02:59.973 回答
0

要点是在 if 或任何循环的循环中实现 MediaPlayer

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.buttonPlay:
        //mp.reset();
        mp=MediaPlayer.create(Ur Context.this, R.raw."ursong.mp3");
        mp.start();

        break;
    case R.id.buttonStop:
        mp.stop();

        break;
    }
}
于 2015-10-04T11:59:59.613 回答