MediaPlayer未暂停音频文件

时间:2017-03-06 10:38:35

标签: android audio media-player

我有一个录音应用程序,它将通过麦克风录制音频,录制音频后,当用户按下播放按钮时,它将播放音频,直到这里它工作得很好,但我想在播放时暂停音频文件用户单击暂停按钮。这是我的java代码。怎么做?

public class tab1 extends Fragment {

private ViewPager mViewPager;
private Button play,stop,record;
private MediaRecorder myAudioRecorder;
private String outputfile;


public tab1() {

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    final View rootview=inflater.inflate(R.layout.fragment_tab1,container,false);


    final ImageButton  record=(ImageButton)rootview.findViewById(R.id.record);
   final ImageButton stop=(ImageButton) rootview.findViewById(R.id.stop);
   final ImageButton play=(ImageButton) rootview.findViewById(R.id.play);
    final  ImageButton pause=(ImageButton)rootview.findViewById(R.id.pause);


 stop.setVisibility(getView().INVISIBLE);


    File directorysample=new File(Environment.getExternalStorageDirectory()+separator+"RadioRecords");
    directorysample.mkdirs();
    String filename = "/recording_" + System.currentTimeMillis() + ".mp3";
    final  String directory = Environment.getExternalStorageDirectory()+ "/RadioRecords";
    outputfile = directory + "/" + filename;




    record.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {


                myAudioRecorder=new MediaRecorder();
                myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.DEFAULT);
                myAudioRecorder.setOutputFile(outputfile);
                myAudioRecorder.prepare();
                myAudioRecorder.start();
            }
            catch (IllegalStateException ise){



            }catch (IOException ioe){



            }
         // record.setEnabled(false);
          //stop.setEnabled(true);
            stop.setVisibility(getView().VISIBLE);


        }

    });


    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            myAudioRecorder.stop();
        //   record.setEnabled(true);
            myAudioRecorder.release();
            myAudioRecorder=null;
         //stop.setEnabled(false);
       //play.setEnabled(true);


            stop.setVisibility(getView().INVISIBLE);

        }

    });
    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MediaPlayer mediaPlayer=new MediaPlayer();

            try {
                mediaPlayer.setDataSource(outputfile);
                mediaPlayer.prepare();
                mediaPlayer.start();


            }
            catch (Exception e)
            {

            }

        }
    });

    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MediaPlayer mediaPlayer=new MediaPlayer();

            try {
                mediaPlayer.setDataSource(outputfile);
                mediaPlayer.prepare();
                mediaPlayer.pause();


            }
            catch (Exception e)
            {

            }

        }
    });
    return rootview;
}

 }

1 个答案:

答案 0 :(得分:1)

请勿使用多个MediaPlayers进行播放和暂停。使用One MediaPlayer。在onCreateView中启动它或者在onClick上播放并使用你想要的地方。

全局声明一个MediaPlayer:

MediaPlayer mediaPlayer;

在onCreateView中启动它或者播放onClick:

mediaPlayer = new MediaPlayer();

并替换它:

mediaPlayer.setDataSource(outputfile);
mediaPlayer.prepare();
mediaPlayer.pause();

使用:

if (mediaPlayer.isPlaying()) {
     mediaPlayer.pause();
}