2011-11-01 64 views
0
package com.Project_recording; 

import java.io.File; 
import java.io.IOException; 

import android.app.Activity; 
import android.media.MediaPlayer; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class Project_recordingActivity extends Activity { 
    private static final String APP_TAG = "com.hascode.android.soundrecorder"; 

    private MediaRecorder recorder = new MediaRecorder(); 
    private MediaPlayer player = new MediaPlayer(); 

    private Button btRecord; 
    private Button btPlay; 
    private TextView resultView; 

    private boolean recording = false; 
    private boolean playing = false; 
    private File outfile = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     resultView = (TextView) findViewById(R.id.output); 

     try { 
      // the soundfile 
      File storageDir = new File(Environment 
        .getExternalStorageDirectory(), "com.hascode.recorder"); 
      storageDir.mkdir(); 
      Log.d(APP_TAG, "Storage directory set to " + storageDir); 
      outfile = File.createTempFile("hascode", ".3gp", storageDir); 

      // init recorder 
      recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
      recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
      recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
      recorder.setOutputFile(outfile.getAbsolutePath()); 

      // init player 
      player.setDataSource(outfile.getAbsolutePath()); 
     } catch (IOException e) { 
      Log.w(APP_TAG, "File not accessible ", e); 
     } catch (IllegalArgumentException e) { 
      Log.w(APP_TAG, "Illegal argument ", e); 
     } catch (IllegalStateException e) { 
      Log.w(APP_TAG, "Illegal state, call reset/restore", e); 
     } 

     btRecord = (Button) findViewById(R.id.btRecord); 
     btRecord.setOnClickListener(handleRecordClick); 

     btPlay = (Button) findViewById(R.id.btPlay); 
     btPlay.setOnClickListener(handlePlayClick); 

    } 

    private final OnClickListener handleRecordClick = new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (!recording) { 
       startRecord(); 
      } else { 
       stopRecord(); 
      } 
     } 
    }; 

    private final OnClickListener handlePlayClick = new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (!playing) { 
       startPlay(); 
      } else { 
       stopPlay(); 
      } 
     } 
    }; 

    private void startRecord() { 
     Log.d(APP_TAG, "start recording.."); 
     printResult("start recording.."); 
     try { 
      recorder.prepare(); 
      recorder.start(); 
      recording = true; 
     } catch (IllegalStateException e) { 
      Log 
        .w(APP_TAG, 
          "Invalid recorder state .. reset/release should have been called"); 
     } catch (IOException e) { 
      Log.w(APP_TAG, "Could not write to sd card"); 
     } 
    } 

    private void stopRecord() { 
     Log.d(APP_TAG, "stop recording.."); 
     printResult("stop recording.."); 
     recorder.stop(); 
     recorder.reset(); 
     recorder.release(); 
     recording = false; 
    } 

    private void startPlay() { 
     Log.d(APP_TAG, "starting playback.."); 
     printResult("start playing.."); 
     try { 
      playing = true; 
      player.prepare(); 
      player.start(); 
     } catch (IllegalStateException e) { 
      Log.w(APP_TAG, "illegal state .. player should be reset"); 
     } catch (IOException e) { 
      Log.w(APP_TAG, "Could not write to sd card"); 
     } 
    } 

    private void stopPlay() { 
     Log.d(APP_TAG, "stopping playback.."); 
     printResult("stop playing.."); 
     player.stop(); 
     player.reset(); 
     player.release(); 
     playing = false; 
    } 

    private void printResult(String result) { 
     resultView.setText(result); 
    } 
} 

当我按下录音按钮时,Is开始录音。当我按下播放按钮时,它开始播放。当我再次按下播放按钮时,我停止播放。我所面临的重要问题是声音没有听到..?请帮我一个必要的。我是新来的机器人..录制和播放,但声音不会来?

+0

请帮我..我在过去3周中一直在努力。 – Yasir

+0

你有没有用.mp3扩展名尝试过 – Karthi

+0

正如你所说,我刚刚尝试过,但没有用....任何如何谢谢你你的即时回应...! – Yasir

回答

0

为什么你在记录后立即重置记录器对象。

+0

感谢您的即时回复。我删除了prepare.reset(),但现在仍然面临同样的问题... – Yasir

+0

也许这个链接可能会帮助你,http://www.benmccann.com/dev-blog/android-audio-recording-教程/ – 2011-11-01 13:05:49

+0

对不便之处..其实我现在在办公室..你指定的链接不工作..请做我需要.. :-) – Yasir