录制来电

时间:2012-05-25 09:51:22

标签: android mediarecorder

private void Record() {
    // TODO Auto-generated method stub


    Log.d("message","In Record function.................");

    if(recorder==null)
    {
        recorder = new MediaRecorder();
        try 
        {
            //File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "saw.wav");
            recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            recorder.setOutputFile(Environment.getExternalStorageDirectory().getCanonicalPath() + "saw.ogg" );
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);


            Log.d("message","Recording startedddd......");
             try {
                    recorder.prepare();
                    recorder.start();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            Log.d("message","Recording completed......");


        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            // TODO: handle exception
        }
    }
}

它给出了以下问题:

05-25 09:50:00.117: W/System.err(8323): java.io.IOException: No valid output file
05-25 09:50:00.117: W/System.err(8323):     at android.media.MediaRecorder.prepare(MediaRecorder.java:603)
05-25 09:50:00.117: W/System.err(8323):     at com.android.CallRecord.Listener.Record(Listener.java:73)
05-25 09:50:00.117: W/System.err(8323):     at com.android.CallRecord.Listener.onCallStateChanged(Listener.java:35)
05-25 09:50:00.117: W/System.err(8323):     at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:319)
05-25 09:50:00.117: W/System.err(8323):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 09:50:00.117: W/System.err(8323):     at android.os.Looper.loop(Looper.java:130)
05-25 09:50:00.117: W/System.err(8323):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-25 09:50:00.117: W/System.err(8323):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 09:50:00.117: W/System.err(8323):     at java.lang.reflect.Method.invoke(Method.java:507)
05-25 09:50:00.117: W/System.err(8323):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-25 09:50:00.117: W/System.err(8323):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-25 09:50:00.117: W/System.err(8323):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

试用此代码。使用MediaRecorder.AudioSource.VOICE_CALL

folder = this.getFilesDir().toString();        
    String fname = "";        
    try        
    {   

        android.media.MediaRecorder mr = new MediaRecorder();
        mr.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 
        mr.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        fname = folder + "/aa"+System.currentTimeMillis()+".mp4";
        mr.setOutputFile(fname); 
        mr.prepare();
        mr.start(); 

        /////////////////////////// 

        hold here and make call then release the execution. I tested by debug and add the bread point here. 
        Thread.sleep(1000); 

        ////////////////////////////

        mr.stop();          
        mr.reset();
        mr.release();

        MediaPlayer player = new MediaPlayer();

        FileInputStream fis = new FileInputStream(fname);
        FileDescriptor fd = fis.getFD();
        player.setDataSource(fd);

        player.prepare();
        player.start();         
    }
    catch(Exception ee)
    {
        Log.d(Utils.Tag_Log, ee.toString());
    }