4

我正在開發一個android應用程序,只需使用按鈕開始和停止錄製。 我使用了線程。我創建了三個類..一個開始錄音..一個停止錄音和主類..使用線程的Android媒體錄製

問題是,我可以看到我的手機中的文件,但它是空的,移動給我一個味精「無法播放視頻」 ...我希望它與線程工作..我不想其他方法..

這是我的代碼 主類:

public class MediaRecorderSampleActivity extends Activity { 


    Button start; 
    Button stop ; 
    private MediaRecorder recorder ; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     start = (Button)findViewById(R.id.startbtn); 
     stop = (Button)findViewById(R.id.stopbtn); 
     start.setOnClickListener(new btnClick()); 
     stop.setOnClickListener(new StopbtnClick()); 
     } 



    class btnClick implements View.OnClickListener { 
       public void onClick(View arg0) { 
      Log.i("Media", "Start Clicked..."); 
      Thread startThread = new Thread (new startRe (recorder)); 
      Log.i("Media", "start Thread Created"); 
      startThread.start() ; 
      Log.i("Media", "start Recording"); 

       }   
    } 


    class StopbtnClick implements View.OnClickListener { 
     public void onClick(View arg0) { 
       Log.i("Media", "Stop Clicked..."); 
     // TODO Auto-generated method stub 
      Thread stopThread = new Thread (new stopRecording (recorder)); 
       Log.i("Media", "stop Thread Created"); 
    stopThread.start(); 
    Log.i("Media", "stop Recording"); 
     } 

    } 

     } 

中的startRecording類

public class startRe implements Runnable { 
private MediaRecorder recorder; 


startRe(MediaRecorder r) { 
    Log.i("Media", "start cons"); 
    this.recorder = r ; 

} 
public void run() { 
    // TODO Auto-generated method stub 
    Log.i("Media", "IN RUN start Recording"); 
    startRecording(); 
} 


public void startRecording() { 
    Log.i("Media", "IN Method start Recording"); 
    recorder = new MediaRecorder(); 
    Log.i("Media", "create variable"); 
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    Log.i("Media", "1"); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
    Log.i("Media", "2"); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    Log.i("Media", "3"); 
    recorder.setOutputFile(getFilePath()); 
    try{ 
     Log.i("Media", "prepar"); 
     recorder.prepare(); 
     Log.i("Media", "before"); 
     recorder.start(); 
     Log.i("Media", "after"); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 

} 


private String getFilePath() { 
    String filePath = Environment.getExternalStorageDirectory().getPath(); 
    File file = new File(filePath, "MediaRecorderSample"); 

    if(!file.exists()) 
     file.mkdirs(); 

    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp4"); 
} 


} 

的stopClass

public class stopRecording implements Runnable { 
private MediaRecorder recorder ; 

public stopRecording(MediaRecorder recorder2) { 
    Log.i("Media", "Stop in Cos"); 
    // TODO Auto-generated constructor stub 
    try { 
    this.recorder = recorder2 ; } 
    catch (Exception e) 
    {  
     Log.i("Media", "Stop out Cos" + e.getMessage()) ; 
     } 

} 
public void run() { 
    Log.i("Media", "Stop in RUN"); 
    stopRecording(); 
    Log.i("Media", "Stop out of RUN"); 

} 

回答

4

有一個在你如何使用MediaRecorder的對象問題。您需要在活動類來創建對象,然後將對象傳遞兩個Runnable ..

所以,你需要做以下更改:

創建活動類的對象下面的代碼:

private MediaRecorder recorder ; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     start = (Button)findViewById(R.id.startbtn); 
     stop = (Button)findViewById(R.id.stopbtn); 
     start.setOnClickListener(new btnClick()); 
     stop.setOnClickListener(new StopbtnClick()); 
     // Create the object in Activity so that both Runnable works on the same object... 
     recorder = new MediaRecorder(); 
     } 

將相同的對象傳遞給Runnable類,就像您已經在做的那樣。

不要建立在startRecording()方法的對象,因爲它會創建一個本地對象,並將其分配給不會從stopRecordingRunnable訪問的局部變量..

public void startRecording() { 
    Log.i("Media", "IN Method start Recording"); 
    // comment this recorder = new MediaRecorder(); 
    Log.i("Media", "create variable"); 
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    Log.i("Media", "1"); 
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
    Log.i("Media", "2"); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
    Log.i("Media", "3"); 
    recorder.setOutputFile(getFilePath()); 
    try{ 
     Log.i("Media", "prepar"); 
     recorder.prepare(); 
     Log.i("Media", "before"); 
     recorder.start(); 
     Log.i("Media", "after"); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 

} 

嘗試,讓我們知道結果...

+0

Thaanks,它的工作.. – TravellingSalesWoman

+0

很高興我可以幫助... –