通话录音应用

时间:2017-12-22 19:52:56

标签: android broadcastreceiver

我一直试图在我的MI note 3手机中录制audion电话,但我只能录制一次电话,如果我尝试尝试录制第二个电话,则没有任何反应。我认为问题出在接收器上。

    public class MainActivity extends AppCompatActivity
    {
        Button startButton, stopButton;

        SharedPreferences sharedPreferences;
        SharedPreferences.Editor editor;
        AudioRecorder audioRecorder;
        int permission_All=1;
        String[] Permissions=


     {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_PHONE_STATE};

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

// I used sharedPreference to store the state of the broadcast receiver.
sharedPreferences=getSharedPreferences("MyRecorder",MODE_PRIVATE);
            editor=sharedPreferences.edit();
            editor.putString("stop","NVR");
            editor.commit();

            startButton=findViewById(R.id.startRecording);
            stopButton=findViewById(R.id.stopRecording);
            TelephonyManager telephonyManager =
                    (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

                audioRecorder = new AudioRecorder("myrecoder/filerecorder.3gp");


// checking permissions
            if(!hasPermissions(this,Permissions))
            {
                ActivityCompat.requestPermissions(this,Permissions,permission_All);
            }

                PhoneStateListener callStateListener = new PhoneStateListener() {
                    public void onCallStateChanged(int state, String incomingNumber) {
                        if (state == TelephonyManager.CALL_STATE_RINGING) {
                            Toast.makeText(getApplicationContext(), "Phone Is Riging",
                                    Toast.LENGTH_LONG).show();
                        }

                        if (state == TelephonyManager.CALL_STATE_OFFHOOK)
                        {
                            if (sharedPreferences.contains("stop"))
                            {
                                try
                                {
                                    String check=sharedPreferences.getString("stop","");
                                    if(check.equalsIgnoreCase("NVR"))
                                    {
                                        editor.putString("stop", "VR");
                                        editor.commit();
                                        start();
                                    }

                                } catch (Exception e)
                                {

                                    e.printStackTrace();
                                }
                            }
                        }

                        if (state == TelephonyManager.CALL_STATE_IDLE) {
                            if (sharedPreferences.contains("stop")) {
                                try {

                                    String check=sharedPreferences.getString("stop","");
                                    if(check.equalsIgnoreCase("VR"))
                                    {
                                        editor.putString("stop", "NVR");
                                        editor.commit();
                                        stop();
                                        Toast.makeText(getApplicationContext(), "Phone is in idle state", Toast.LENGTH_LONG).show();
                                    }
                                } catch (Exception e) {

                                    e.printStackTrace();
                                }
                            }

                        }
                    }
                };
                telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

            }

        public void start()
        {
            try {
                audioRecorder.start();
                Toast.makeText(getApplicationContext(),"voice being recorded",Toast.LENGTH_LONG).show();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }

        public void stop()
        {  try {
            audioRecorder.stop();
            Toast.makeText(this,"Voice recording stopping now",Toast.LENGTH_LONG).show();
        }
        catch (Exception e)
        {

            e.printStackTrace();
        }
        }

        public static boolean hasPermissions(Context context, String ...permissions)
        {
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M && context!=null && permissions!=null)
            {
                for (String permission: permissions) {
                    if (ActivityCompat.checkSelfPermission(context, permission)!=PackageManager.PERMISSION_GRANTED)
                    {
                            return  false;
                    }
                }
            }
            return true;
        }
    }
    ## My Audio Recorder file ##
    public class AudioRecorder {

        final MediaRecorder recorder = new MediaRecorder();
        final String path;

        /**
         * Creates a new audio recording at the given path (relative to root of SD card).
         */
        public AudioRecorder(String path) {
            this.path = sanitizePath(path);
        }

        private String sanitizePath(String path) {
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
            if (!path.contains(".")) {
                path += ".3gp";
            }
            return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
        }

        /**
         * Starts a new recording.
         */
        public void start() throws IOException {
            String state = android.os.Environment.getExternalStorageState();
            if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
                throw new IOException("SD Card is not mounted.  It is " + state + ".");
            }

            // make sure the directory we plan to store the recording in exists
            File directory = new File(path).getParentFile();
            if (!directory.exists() && !directory.mkdirs()) {
                throw new IOException("Path to file could not be created.");
            }

            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(path);
            recorder.prepare();
            recorder.start();
        }

        /**
         * Stops a recording that has been previously started.
         */
        public void stop() throws IOException {
            recorder.stop();
            recorder.release();
        }

    }

我的清单文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.rso.newrecorder">

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.RECORD_AUDIO"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

0 个答案:

没有答案