简体   繁体   English

Android中通话录音的代码

[英]Codes for call recording in android

I am making and application that records calls. 我正在制作和记录通话的应用程序。 Here is my code to record a call and save the file in the SD card under songs folder. 这是我的代码,用于记录通话并将文件保存在歌曲文件夹下的SD卡中。 But the problem is that this code was working fine but later it is not working. 但是问题在于此代码可以正常工作,但后来却无法正常工作。 I cannot find what is the problem. 我找不到问题所在。 Can you please help me out? 你能帮我吗?

My Broad cast receiver: 我的广泛演员:

public class BrCallReceive extends BroadcastReceiver {

      @Override
    public void onReceive(Context c, Intent i) {

        Bundle extras = i.getExtras();
        Intent x = new Intent (c, EavesDropperActivity.class);
        x.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.w("DEBUG", state);
            if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                Log.w("DEBUG", "MATCHES");
                Toast.makeText(c, "Launching record APP !", Toast.LENGTH_LONG).show();
                c.startActivity(x);
            }
        }
    }
}   

My Recording activity: 我的录音活动:

public class EavesDropperActivity extends Activity {

    /** Called when the activity is first created. */
    MediaRecorder m_recorder = new MediaRecorder();
    TelephonyManager t_manager ;
    PhoneStateListener p_listener ;
    String record_state;
    Uri file;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        Toast.makeText(getBaseContext(),
                "Executing Activity",
                Toast.LENGTH_LONG).show();

        t_manager = (TelephonyManager) getSystemService (Context.TELEPHONY_SERVICE);
        p_listener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged (int state, String incomingNumber) {
                switch (state) {
                    case (TelephonyManager.CALL_STATE_IDLE)     :
                        stop_recorder();

                        //t_manager.listen(p_listener, PhoneStateListener.LISTEN_NONE);
                        //finish();
                        break;

                    case (TelephonyManager.CALL_STATE_OFFHOOK)     :

                        start_recorder();

                        break;
                    case (TelephonyManager.CALL_STATE_RINGING)     :

                        break;

                }
            }
        };
        t_manager.listen(p_listener, PhoneStateListener.LISTEN_CALL_STATE);
        return;
    }

    public void start_recorder () {
        m_recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        m_recorder.setOutputFormat(OutputFormat.THREE_GPP);
        m_recorder.setOutputFile(Environment.getExternalStorageDirectory()+"/songs/audionew.3gpp");
        m_recorder.setAudioEncoder(AudioEncoder.AMR_NB);

        try {
            m_recorder.prepare();
            m_recorder.start();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void stop_recorder () {
        m_recorder.stop();
        m_recorder.release();
        Uri file = Uri.fromFile(
                new File(Environment.getExternalStorageDirectory(),"/songs/audionew.3gpp"));
        Toast.makeText(getBaseContext(),
                "record stored at " + file.toString(),
                Toast.LENGTH_LONG).show();

        t_manager.listen(p_listener, PhoneStateListener.LISTEN_NONE);
        finish();
    }
}

My manifest : 我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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" />    

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testapp.EavesDropperActivity"
            android:label="@string/app_name" >
    </activity>

    <receiver android:name="BrCallReceive" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" >
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

I think the broadcast receiver is not getting activated wile i am calling my phone. 我认为我正在打电话时,广播接收器并未启动。

How do you know your broadcast receiver is not working? 您怎么知道您的广播接收器不起作用? Place a Log message inside onReceive() right at the beginning. 将日志消息放在开头的onReceive()内。 It might be that your extras == null 可能是您的extras == null

But how to make my receiver functional now? 但是,现在如何使我的接收器正常工作?

Check out example #5 and just follow the same steps. 查看示例5,然后执行相同的步骤。

Bro i dont think activity will get launched. 兄弟,我认为活动不会启动。

Well, it does: You are starting it in your onReceive(): 好吧,它确实是:您正在onReceive()中启动它:

Intent x = new Intent (c, EavesDropperActivity.class);
c.startActivity(x);

However, you are not setting any content in your Activity ie no UI screen is presented because you have this line commented in onCreate() of EavesDropperActivity Activity: 但是,您没有在“活动”中设置任何内容,即未显示任何UI屏幕,因为您在EavesDropperActivity活动的onCreate()中注释了以下行:

//setContentView(R.layout.main);

So, you need to think hard on what you are trying to achieve in EavesDropperActivity 因此,您需要认真思考您要在EavesDropperActivity实现的EavesDropperActivity

HTH. HTH。

Put this 放这个

  <android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>


<action android:name="android.intent.action.QUICKBOOT_POWERON" />

in mainfest file for broadcast receiver. 在mainfest文件中,用于广播接收器。

Add your package name to your receiver class as, 将您的包裹名称添加到您的接收器类中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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" />    

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testapp.EavesDropperActivity"
            android:label="@string/app_name" >
    </activity>

    <receiver android:name="com.example.testapp.BrCallReceive" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" >
                </action>
            </intent-filter>
        </receiver>
    </application>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM