2017-12-11 23 views
2

수신 통화가있을 때 음성을 녹음하려면이 링크를 사용하고 있습니다. Link Url삼성 S7, S8 및 Gionee A1에서 수신 음성이 녹음되지 않음

이 방법은 내가 삼성 S7, S8Gionee A1

상대방의 음성이 기록되지 않고에이 코드를 실행하는 경우를 제외하고 모든 장치와 함께 작동합니다.

+0

은'오류 log'은 무엇입니까? –

+0

오류 로그가 없습니다. 음성을 파일로 저장하고 저장합니다. 또한 내 장치에서 음성을 녹음하지만 수신 전화 음성은 녹음하지 않습니다. –

답변

0

아래 코드를 사용하면 모든 장치에서 제대로 작동합니다.

import java.io.File; 
import java.io.IOException; 
import java.lang.Exception; 
import java.util.Date; 
import java.text.SimpleDateFormat; 

import android.os.IBinder; 
import android.app.Service; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.preference.PreferenceManager; 
import android.content.SharedPreferences; 
import android.content.Context; 
import android.content.Intent; 
import android.media.MediaRecorder; 
import android.widget.Toast; 
import android.util.Log; 

//import java.security.KeyPairGenerator; 
//import java.security.KeyPair; 
//import java.security.Key; 

import java.io.InputStream; 
import java.io.FileInputStream; 
import java.util.Iterator; 

public class RecordService 
    extends Service 
    implements MediaRecorder.OnInfoListener, MediaRecorder.OnErrorListener 
{ 
    private static final String TAG = "CallRecorder"; 

    public static final String DEFAULT_STORAGE_LOCATION = "/sdcard/callrecorder"; 
    private static final int RECORDING_NOTIFICATION_ID = 1; 

    private MediaRecorder recorder = null; 
    private boolean isRecording = false; 
    private File recording = null;; 

    /* 
    private static void test() throws java.security.NoSuchAlgorithmException 
    { 
     KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); 
     kpg.initialize(2048); 
     KeyPair kp = kpg.genKeyPair(); 
     Key publicKey = kp.getPublic(); 
     Key privateKey = kp.getPrivate(); 
    } 
    */ 

    private File makeOutputFile (SharedPreferences prefs) 
    { 
     File dir = new File(DEFAULT_STORAGE_LOCATION); 

     // test dir for existence and writeability 
     if (!dir.exists()) { 
      try { 
       dir.mkdirs(); 
      } catch (Exception e) { 
       Log.e("CallRecorder", "RecordService::makeOutputFile unable to create directory " + dir + ": " + e); 
       Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to create the directory " + dir + " to store recordings: " + e, Toast.LENGTH_LONG); 
       t.show(); 
       return null; 
      } 
     } else { 
      if (!dir.canWrite()) { 
       Log.e(TAG, "RecordService::makeOutputFile does not have write permission for directory: " + dir); 
       Toast t = Toast.makeText(getApplicationContext(), "CallRecorder does not have write permission for the directory directory " + dir + " to store recordings", Toast.LENGTH_LONG); 
       t.show(); 
       return null; 
      } 
     } 

     // test size 

     // create filename based on call data 
     //String prefix = "call"; 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-SS"); 
     String prefix = sdf.format(new Date()); 

     // add info to file name about what audio channel we were recording 
     int audiosource = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_SOURCE, "1")); 
     prefix += "-channel" + audiosource + "-"; 

     // create suffix based on format 
     String suffix = ""; 
     int audioformat = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_FORMAT, "1")); 
     switch (audioformat) { 
     case MediaRecorder.OutputFormat.THREE_GPP: 
      suffix = ".3gpp"; 
      break; 
     case MediaRecorder.OutputFormat.MPEG_4: 
      suffix = ".mpg"; 
      break; 
     case MediaRecorder.OutputFormat.RAW_AMR: 
      suffix = ".amr"; 
      break; 
     } 

     try { 
      return File.createTempFile(prefix, suffix, dir); 
     } catch (IOException e) { 
      Log.e("CallRecorder", "RecordService::makeOutputFile unable to create temp file in " + dir + ": " + e); 
      Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to create temp file in " + dir + ": " + e, Toast.LENGTH_LONG); 
      t.show(); 
      return null; 
     } 
    } 

    public void onCreate() 
    { 
     super.onCreate(); 
     recorder = new MediaRecorder(); 
     Log.i("CallRecorder", "onCreate created MediaRecorder object"); 
    } 

    public void onStart(Intent intent, int startId) { 
     //Log.i("CallRecorder", "RecordService::onStart calling through to onStartCommand"); 
     //onStartCommand(intent, 0, startId); 
     //} 

     //public int onStartCommand(Intent intent, int flags, int startId) 
     //{ 
     Log.i("CallRecorder", "RecordService::onStartCommand called while isRecording:" + isRecording); 

     if (isRecording) return; 

     Context c = getApplicationContext(); 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); 

     Boolean shouldRecord = prefs.getBoolean(Preferences.PREF_RECORD_CALLS, false); 
     if (!shouldRecord) { 
      Log.i("CallRecord", "RecordService::onStartCommand with PREF_RECORD_CALLS false, not recording"); 
      //return START_STICKY; 
      return; 
     } 

     int audiosource = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_SOURCE, "1")); 
     int audioformat = Integer.parseInt(prefs.getString(Preferences.PREF_AUDIO_FORMAT, "1")); 

     recording = makeOutputFile(prefs); 
     if (recording == null) { 
      recorder = null; 
      return; //return 0; 
     } 

     Log.i("CallRecorder", "RecordService will config MediaRecorder with audiosource: " + audiosource + " audioformat: " + audioformat); 
     try { 
      // These calls will throw exceptions unless you set the 
      // android.permission.RECORD_AUDIO permission for your app 
      recorder.reset(); 
      recorder.setAudioSource(audiosource); 
      Log.d("CallRecorder", "set audiosource " + audiosource); 
      recorder.setOutputFormat(audioformat); 
      Log.d("CallRecorder", "set output " + audioformat); 
      recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
      Log.d("CallRecorder", "set encoder default"); 
      recorder.setOutputFile(recording.getAbsolutePath()); 
      Log.d("CallRecorder", "set file: " + recording); 
      //recorder.setMaxDuration(msDuration); //1000); // 1 seconds 
      //recorder.setMaxFileSize(bytesMax); //1024*1024); // 1KB 

      recorder.setOnInfoListener(this); 
      recorder.setOnErrorListener(this); 

      try { 
       recorder.prepare(); 
      } catch (java.io.IOException e) { 
       Log.e("CallRecorder", "RecordService::onStart() IOException attempting recorder.prepare()\n"); 
       Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to start recording: " + e, Toast.LENGTH_LONG); 
       t.show(); 
       recorder = null; 
       return; //return 0; //START_STICKY; 
      } 
      Log.d("CallRecorder", "recorder.prepare() returned"); 

      recorder.start(); 
      isRecording = true; 
      Log.i("CallRecorder", "recorder.start() returned"); 
      updateNotification(true); 
     } catch (java.lang.Exception e) { 
      Toast t = Toast.makeText(getApplicationContext(), "CallRecorder was unable to start recording: " + e, Toast.LENGTH_LONG); 
      t.show(); 

      Log.e("CallRecorder", "RecordService::onStart caught unexpected exception", e); 
      recorder = null; 
     } 

     return; //return 0; //return START_STICKY; 
    } 

    public void onDestroy() 
    { 
     super.onDestroy(); 

     if (null != recorder) { 
      Log.i("CallRecorder", "RecordService::onDestroy calling recorder.release()"); 
      isRecording = false; 
      recorder.release(); 
      Toast t = Toast.makeText(getApplicationContext(), "CallRecorder finished recording call to " + recording, Toast.LENGTH_LONG); 
      t.show(); 

      /* 
      // encrypt the recording 
      String keyfile = "/sdcard/keyring"; 
      try { 
       //PGPPublicKey k = readPublicKey(new FileInputStream(keyfile)); 
       test(); 
      } catch (java.security.NoSuchAlgorithmException e) { 
       Log.e("CallRecorder", "RecordService::onDestroy crypto test failed: ", e); 
      } 
      //encrypt(recording); 
      */ 
     } 

     updateNotification(false); 
    } 


    // methods to handle binding the service 

    public IBinder onBind(Intent intent) 
    { 
     return null; 
    } 

    public boolean onUnbind(Intent intent) 
    { 
     return false; 
    } 

    public void onRebind(Intent intent) 
    { 
    } 


    private void updateNotification(Boolean status) 
    { 
     Context c = getApplicationContext(); 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); 

     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     if (status) { 
      int icon = R.drawable.rec; 
      CharSequence tickerText = "Recording call from channel " + prefs.getString(Preferences.PREF_AUDIO_SOURCE, "1"); 
      long when = System.currentTimeMillis(); 

      Notification notification = new Notification(icon, tickerText, when); 

      Context context = getApplicationContext(); 
      CharSequence contentTitle = "CallRecorder Status"; 
      CharSequence contentText = "Recording call from channel..."; 
      Intent notificationIntent = new Intent(this, RecordService.class); 
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
      mNotificationManager.notify(RECORDING_NOTIFICATION_ID, notification); 
     } else { 
      mNotificationManager.cancel(RECORDING_NOTIFICATION_ID); 
     } 
    } 

    // MediaRecorder.OnInfoListener 
    public void onInfo(MediaRecorder mr, int what, int extra) 
    { 
     Log.i("CallRecorder", "RecordService got MediaRecorder onInfo callback with what: " + what + " extra: " + extra); 
     isRecording = false; 
    } 

    // MediaRecorder.OnErrorListener 
    public void onError(MediaRecorder mr, int what, int extra) 
    { 
     Log.e("CallRecorder", "RecordService got MediaRecorder onError callback with what: " + what + " extra: " + extra); 
     isRecording = false; 
     mr.release(); 
    } 
} 

참조 .->https://github.com/esnyder/callrecorder

+0

감사합니다.하지만이 코드는 삼성 S7에서 들어오는 음성을 녹음하지 않습니다. 또한 미디어 레코더 및 오디오 레코더 클래스를 시도했지만 아무것도 작동하지 않았습니다. 할 수있는 일이 있으면 알려주세요. –

+0

나는 더 많은 것을 시도 할 것이고, 내가 무언가를 찾으면 당신에게 알릴 것이다. –

+0

덕분에 Chandrakant는 좋지만 extensivley를 보길 바란다. –