首页 > 解决方案 > 录音机第一次工作,然后只记录3秒

问题描述

在这段代码中,每当我首先安装应用程序时,它都能正常工作,但是当我反复使用它来记录通话时,它不能正常工作,它只会记录 3 秒的音频。所以我想知道我犯了什么错误。如果有任何问题,请告诉我。音频正在正确录制,但之后音频文件只有 3 秒的音频。

public class MyPhoneStateListener extends PhoneStateListener {

    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static boolean status;
    MediaRecorder media;

    private void startrecorder(boolean status) {
       if(status) {
           media = new MediaRecorder();
           String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
                   + UUID.randomUUID().toString() + "record.3gp";
           media.setAudioSource(MediaRecorder.AudioSource.MIC);
           media.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
           media.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
           media.setOutputFile(path);
           try {
               media.prepare();
               media.start();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       else{
           media.stop();

       }
    }



    public void onCallStateChanged(Context context, int state, String phoneNumber){

        if(lastState == state){
//No change, debounce extras
            return;
        }

        System.out.println("Number inside onCallStateChange : "  + phoneNumber);
        
        switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                startrecorder(true);

                Toast.makeText(context, "Incoming Call Ringing " + phoneNumber, Toast.LENGTH_SHORT).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:

                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    startrecorder(true);
                    callStartTime = new Date();
                    Toast.makeText(context, "Outgoing Call Started " + phoneNumber, Toast.LENGTH_SHORT).show();
                }
                break;

            case TelephonyManager.CALL_STATE_IDLE:
//Went to idle-  this is the end of a call.  What type depends on previous state(s)
                startrecorder(false);
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
//Ring but no pickup-  a miss
                    Toast.makeText(context, "Ringing but no pickup" + phoneNumber + " Call time " + callStartTime +" Date " + new Date() , Toast.LENGTH_SHORT).show();
                }
                else if(isIncoming){
                    Toast.makeText(context, "Incoming " + phoneNumber + " Call time " + callStartTime  , Toast.LENGTH_SHORT).show();

                }
                else{

                    Toast.makeText(context, "outgoing " + phoneNumber + " Call time " + callStartTime +" Date " + new Date() , Toast.LENGTH_SHORT).show();

                }

                break;
        }
        lastState = state;
    }
}

标签: javaandroid

解决方案


对于视频,通常有一个 blob 数组,不确定这个实现有何不同,因为我没有看到数组。
尝试使用media.start(100);,它可能会工作。


推荐阅读