Seekbar在媒体播放器实现方面没有进展

时间:2014-03-31 11:19:15

标签: android audio media-player playback seekbar

我试过这段代码用seekbar实现音频播放。但是搜索栏没有被调用。任何人都告诉我我错在哪里?我想将录制时间限制设置为2分钟。这是我的代码,

   @Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.option4);

    startPlaying = (Button) findViewById(R.id.play);
    stopPlaying = (Button) findViewById(R.id.stop);
    startRecord = (Button) findViewById(R.id.start);
    seekBar = (SeekBar) findViewById(R.id.seek_bar);
    seekBar.setOnSeekBarChangeListener(this);
    seekBar.setEnabled(false);


    startRecord.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

              ditchMediaRecorder();
           mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
              mFileName += "/audiorecordtest.3gp";
              File outputFile=new File(mFileName);
            if(outputFile.exists()){
               outputFile.delete();
            }
            mRecorder=new MediaRecorder();
             mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
             mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

             mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
             mRecorder.setOutputFile(mFileName);
             try {
                mRecorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


             mRecorder.start();
        }
    });  



    startPlaying.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             ditchMediaPlayer();
           mPlayer=new MediaPlayer();
           try {
            mPlayer.setDataSource(mFileName);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           try {
            mPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         seekBar.setEnabled(true);
         seekBar.setMax(mPlayer.getDuration());

           mPlayer.start();


        new Thread().start();

        }
    });

    stopPlaying.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(mRecorder!=null){
                mRecorder.stop();
            }
        }
    });

}


  private void ditchMediaPlayer() {
  if(mPlayer!=null){
mPlayer.release();
    }
    }


     private void ditchMediaRecorder() {
// TODO Auto-generated method stub
if(mRecorder!=null){
    mRecorder.release();
}

       }

           @Override
           public void run() {
  int currentPosition = mPlayer.getCurrentPosition();
  int total = mPlayer.getDuration();
   System.out.println("In run");
  while (mPlayer != null && currentPosition < total) {
   try {
    Thread.sleep(1000);
    System.out.println("In run try block");
    currentPosition = mPlayer.getCurrentPosition();
   } catch (InterruptedException e) {
    return;
   } catch (Exception e) {
    return;
   }
   seekBar.setProgress(currentPosition);
  }

      }

        @Override
       public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
 try {
     System.out.println("In progres");
       if (mPlayer.isPlaying() || mPlayer != null) {
        if (fromUser)
         mPlayer.seekTo(progress);
       } else if (mPlayer== null) {

        seekBar.setProgress(0);
       }
      } catch (Exception e) {
       Log.e("seek bar", "" + e);
       seekBar.setEnabled(false);
  } 
      }

音频将被录制和播放,但我的搜索栏无法控制。我尝试了不同的方式,但我没有成功。请帮助我!!

感谢!!!

1 个答案:

答案 0 :(得分:0)

这对我有用:

public void forwardSong() {
if (mPlayer != null) {
    int currentPosition = mPlayer.getCurrentPosition();
    if (currentPosition + seekForwardTime <= mPlayer.getDuration()) {
        mPlayer.seekTo(currentPosition + seekForwardTime);
    } else {
        mPlayer.seekTo(mPlayer.getDuration());
    }
}
}

另外,请查看以下链接: