Android通话录音机应用

时间:2015-06-19 11:09:40

标签: java android android-intent

我开发了基本的呼叫记录器应用程序,它在具有以下版本的设备上运行良好&#39;棒棒糖&#39;,但是当我必须运行棒棒糖设备(Android 5.0)时,我收到此错误。< / p>

E/MediaRecorder(29786): start failed: -2147483648
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.androiddoc.callRecorder_anirbanjana.CallRecordingService.startRec(CallRecordingService.java:234)
at com.androiddoc.callRecorder_anirbanjana.CallRecordingService.onCreate(CallRecordingService.java:54)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2763)
at android.app.ActivityThread.access$1800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

我的代码如下。当我调用recorder.prepare();和recorder.start();现在我遇到了错误,我无法解决原因。有人可以建议吗?

public class CallRecordingService extends Service {

    MediaRecorder myRec = new MediaRecorder();
    boolean isRecMode = false;

    private static final int RECORDER_SAMPLERATE = 8000;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    private AudioRecord recorder = null;
    private Thread recordingThread = null;
    private boolean isRecording = false;

    int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY_COMPATIBILITY;
    }

    @Override
    public void onCreate() {
        // Start recording
         startRec();


        System.out.println("BUFFER SIZE VALUE IS " + bufferSize);
        super.onCreate();
    }

    public void stopRec() {
        myRec.reset();
        Toast.makeText(CallRecordingService.this, "Rec Stoped",
                Toast.LENGTH_SHORT).show();
        Log.i("CallRecordingService", "Rec Stoped");
        isRecMode = false;
    }


    public void startRec() {
        try {
            // String selectedPath = this.getApplicationContext().getFilesDir()
            // + "/system_sound";
            String selectedPath = Environment.getExternalStorageDirectory()
                    + "/Testing";

            SimpleDateFormat sdf = new SimpleDateFormat(
                    "dd_MM_yyyy_hh_mm_ss aa");
            String currentDateandTime = sdf.format(new Date());

            // Crate folder if not exist
            // String packageName = this.getPackageName();
            File yourDir = new File(Environment.getExternalStorageDirectory()
                    + "/Testing");
            // File yourDir = new
            // File(this.getApplicationContext().getFilesDir() +
            // "/system_sound");
            if (!yourDir.exists()) {
                yourDir.mkdirs();
            }
            // String selectedPath =
            // Environment.getExternalStorageDirectory().getAbsolutePath() +
            // "/Android/data/" + packageName + "/system_sound";

            myRec.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
            myRec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myRec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            // myRec.setOutputFile(Environment.getExternalStorageDirectory().getPath()+"/my_rec_voice.mp3");
            myRec.setOutputFile(selectedPath + "/" + currentDateandTime + "_"
                    + getFromPreference("phoneNo") + ".amr");

            AudioManager am; // Audio manager

            am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            am.setMode(AudioManager.MODE_IN_CALL);
            int volume_level = am
                    .getStreamVolume(AudioManager.STREAM_VOICE_CALL);
            int max_volume = am
                    .getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
            if (volume_level < max_volume) {
                am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, max_volume,
                        AudioManager.FLAG_SHOW_UI);
            }
        } catch (IllegalStateException e) {
            // Log.e("Call recorder IllegalStateException: ", "");
            terminateAndEraseFile();
        } catch (Exception e) {
            // Log.e("Call recorder Exception: ", "");
            terminateAndEraseFile();
        }

        OnErrorListener errorListener = new OnErrorListener() {

            public void onError(MediaRecorder arg0, int arg1, int arg2) {
                terminateAndEraseFile();
            }

        };
        myRec.setOnErrorListener(errorListener);

        try {
            Log.v("Call Recorder", "Media recorder prepared");
            myRec.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Log.v("Call Recorder", "Media recorder started ");
            myRec.start();

        } catch (Exception e) {
            e.printStackTrace();
        }

        Toast.makeText(CallRecordingService.this, "Rec Start",
                Toast.LENGTH_SHORT).show();
        Log.i("CallRecordingService", "Rec Start");
        isRecMode = true;
    }

    @Override
    public void onDestroy() {
         stopRec();
        //stopRecording();
        if (isRecMode) {
            // Stop recording
            myRec.stop();
        }

        myRec.release();
        super.onDestroy();
    }

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Preference Variable
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    // --------------------------------------------
    // method to save variable in preference
    // --------------------------------------------
    public void saveInPreference(String name, String content) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(name, content);
        editor.commit();
    }

    // --------------------------------------------
    // getting content from preferences
    // --------------------------------------------
    public String getFromPreference(String variable_name) {
        String preference_return;
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        preference_return = preferences.getString(variable_name, "");

        return preference_return;
    }

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Preference Variable
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    private void terminateAndEraseFile() {
        try {

            Toast toast = Toast.makeText(this,
                    this.getString(R.string.reciever_end_call),
                    Toast.LENGTH_SHORT);
            toast.show();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

        Toast toast = Toast.makeText(this,
                this.getString(R.string.record_impossible), Toast.LENGTH_LONG);
        toast.show();
    }

}

1 个答案:

答案 0 :(得分:0)

如果您为spring.task.scheduling.pool.size=10获得了start failed,请使用VOICE_CALL

MIC仅适用于系统应用程序。

这是详细的代码:

VOICE_CALL