Android的通话记录应用,抛出异常

时间:2012-07-16 05:37:03

标签: android record mediarecorder

当我在Emulator中运行以下代码时,调用startes,当调用断开时,调用stop函数时显示NullPointerException,当使用VOICE_CALL作为audiosource时输出为空文件。但是当我将VOICE_UPLINK作为音频源时仍然有效(仍然存在nullpointer异常)。当我在设备中运行它时,它会在拨打号码时显示FileNotFound异常和Permission Denied。但我已在清单文件中提供了所需的权限。

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

我只从互联网上获取代码。我制作的那个是完全的灾难..完整的类代码:

public class ServiceReceiver extends BroadcastReceiver {
PhoneStateListener listener; 
TelephonyManager telephony;
MediaRecorder recorder = new MediaRecorder();

@Override
public void onReceive(Context context, Intent intent) {

    telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

    listener = new PhoneStateListener() {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            switch(state)
            {

               case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d("DEBUG", "OFFHOOK");        
                    StartRecording();
                    break;
               case TelephonyManager.CALL_STATE_IDLE:
                   Log.d("DEBUG", "IDLE");        
                    StopRecording();
                   break;
               case TelephonyManager.CALL_STATE_RINGING:
                   Log.d("DEBUG", "RINGING");
                   break;
            }

        }

        public void StartRecording(){

             recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
             recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
             recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
             recorder.setOutputFile(this.getFullSdPath());

             try {
                recorder.prepare();
                Log.i(this.getClass().getName(), "Prepared");
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.i(this.getClass().getName(), "ERR 1");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.i(this.getClass().getName(), "ERR 2");
            }

            recorder.start();   // Recording is now started
            Log.i(this.getClass().getName(), "Start Recording");
        }

        public void StopRecording(){

             recorder.stop();          
             recorder.release();
             recorder = null;
             Log.i(this.getClass().getName(), "Stop Recording");
        }

        public String getFullSdPath(){
            File sdCard = new File(Environment.getExternalStorageDirectory()    + "/RecordMyVoice");
            if (!sdCard.exists()) {
                sdCard.mkdir();
            }
            File file = new File(Environment.getExternalStorageDirectory()+"/RecordMyVoice/",new Date().getTime()+".mp4");
            System.out.println("Full path of record sound is : "+file.getAbsolutePath());
            return file.getAbsolutePath();
        }           
    };          
    telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);           
}
    }

我做错了吗?我听说在Android中无法轻松制作通话录音应用。

0 个答案:

没有答案