you should also show logcat output.

On Sep 20, 5:35 am, melo <[email protected]> wrote:
> Hi all
> I'm an android starter
> I used MediaRecorder and MediaPlayer to record voice via mic before
> This time,I try AudioRecord and  AudioTrack to record voice
> But when I try to start, it's forced to be shut down
> I've no idea where is wrong?
> here are my code ~
> Could any one tell me why?
> TKS in advance :)
>
> public class audioRecorderTest2 extends Activity
> {
>
>         boolean isRecording = true;
>
>         private static final String Tag = "audioRecorder";
>         private static final boolean log = true;
>
>         Button recordButton;
>         Button stopButton;
>
>         OnClickListener record_listener = null;
>         OnClickListener stop_listener = null;
>
>         int frequency = 8000;
>         int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
>         int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
>
>         protected static final int MENU_ABOUT = Menu.FIRST;
>         protected static final int MENU_Quit = Menu.FIRST + 1;
>
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState)
>     {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>         initResourceRefs();
>
>         setTitle("Audio recorder test...");
>
>         record_listener = new OnClickListener()
>             {
>                 @Override
>                 public void onClick(View arg0)
>                 {
>                         AudioManager am =
> (AudioManager)getSystemService(Context.AUDIO_SERVICE);
>
>                         //int actualBufferSize = 4096*8;
>
>                         int bufferSize =
> AudioTrack.getMinBufferSize(frequency,channelConfiguration,audioEncoding);
>
>                         int actualBufferSize = bufferSize;//4096*8
>
>                         AudioTrack audioTrack = new
> AudioTrack(AudioManager.STREAM_MUSIC,
>                                                                        
> frequency,
>                                                                        
> channelConfiguration,
>                                                                audioEncoding,
>                                                                
> actualBufferSize,
>
> AudioTrack.MODE_STREAM);
>
>                         byte[] buffer = new byte[actualBufferSize];
>
>                         AudioRecord audioRecord = new
> AudioRecord(MediaRecorder.AudioSource.MIC,
>                                                                           
> frequency,
>                                                                           
> channelConfiguration,
>                                                                           
> audioEncoding,
>                                                                   
> actualBufferSize);
>
>                         am.setRouting(AudioManager.MODE_NORMAL,
>                                       AudioManager.ROUTE_EARPIECE,
>                                       AudioManager.ROUTE_ALL);
>
>                         am.setSpeakerphoneOn(true);
>                         am.setMicrophoneMute(false);
>
>                         Log.d(Tag,"Is speakerphone on? : " + 
> am.isSpeakerphoneOn());
>
>                         audioTrack.setPlaybackRate(frequency);
>                         audioRecord.startRecording();
>                         audioTrack.play();
>
>                         int frameSize = bufferSize;//320
>
>                         while(isRecording == true)
>                         {
>                           try{
>                                 int bufferReadResult = 
> audioRecord.read(buffer,
> 0,frameSize);
>
>                                 if(bufferReadResult == 
> AudioRecord.ERROR_INVALID_OPERATION)
>                                         
> if(log)Log.d(Tag,"record.read:ERROR_INVALID_OPERATION
> returned");
>
>                                 else if(bufferReadResult == 
> AudioRecord.ERROR_BAD_VALUE)
>                                 if(log)Log.d(Tag,"record.read:ERROR_BAD_VALUE
> returned");
>
>                                 else if(bufferReadResult != frameSize)
>                                         
> if(log)Log.d(Tag,"record.read:Requested " + frameSize +
>                                                         " bytes but read" + 
> bufferReadResult + "bytes");
>
>                             else audioTrack.write(buffer,0,buffer.length);
>                           }catch(Exception e)
>                           {if(log)Log.e(Tag,"ERROR: " + e.toString());}
>                         }
>
>                         audioRecord.stop();
>                         audioTrack.stop();
>                         audioRecord.release();
>                         audioTrack.release();
>                 }
>             };//record_listener
>
>             stop_listener = new OnClickListener()
>             {
>                 @Override
>                 public void onClick(View arg0)
>                 {
>                         isRecording = false;
>                 }
>             };//stop_listener
>
>            setListeners();
>         }//onCreate
>
>     private void initResourceRefs()//初始化
>     {
>         recordButton = (Button)findViewById(R.id.recordButton);
>         stopButton = (Button)findViewById(R.id.stopButton);
>     }//initResourceRefs()
>
>     private void setListeners()
>     {
>         recordButton.setOnClickListener(record_listener);
>         stopButton.setOnClickListener(stop_listener);
>     }//setListeners
>
>
>
> }//audioRecorderTest2

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to