2014-03-31 90 views
0

我試過這個代碼來實現與seekbar的音頻播放。但是seekbar沒有被調用。任何人都可以告訴我我錯誤的地方嗎?我想將錄音時間限制爲2分鐘。這是我的代碼,Seekbar在媒體播放器執行中沒有進展

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

音頻將被記錄並打得很好,但我的搜索條沒有得到control.I以不同的方式嘗試,但我不succeed.Please幫我!

謝謝!

+0

您可以使用http://www.androidhive。 info/2012/03/android-building-audio-player-tutorial/ –

+0

你有什麼錯誤嗎? – JoelFernandes

+0

@JoelFernandes我粘貼了我的錯誤跟蹤 – micky

回答

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

此外,檢查下面的鏈接:

+0

,我收到錯誤,如錯誤-38,0 – micky