2013-05-17 41 views

回答

0

我認爲你可以使用MediaRecorder.AudioSource.MIC代替。

1

檢查的方法是嘗試記錄並捕獲異常。沒有人提前知道設備是否可以工作。如果發現異常,您可以嘗試從MediaRecorder.AudioSource.MIC開始錄製。如果電話的揚聲器很低,你會發現只有手機擁有者將被錄製,並且線上的另一方不會,但這是你能做的最好的。

我還沒有看到禁用這些功能的手機型號列表。它肯定會是一個方便的列表。

+0

我應該在哪裏嘗試/捕獲異常?是否必須包裝'MediaRecorder.setAudioSource()','MediaRecorder.prepare()','MediaRecorder.start()',或全部? – ocramot

+0

你解決了嗎?我困在這一些地方,請你幫我。 –

0

我做了這個過程: 我試圖在我想要的形狀的try/catch塊中創建一個文件,在開始和停止記錄器中,一切順利,它與所選音頻源一起工作。

public class sRecAudioMic extends Service { 

DecimalFormat fCoordenadas = new DecimalFormat("##.00000000"); 
DecimalFormat fVelocidade = new DecimalFormat("##.0"); 
static String imei = ""; 

private static String FORMATO_AUDIO = ".aac"; 
static String AUDIO_RECORDER_FOLDER = ""; 
static String codigo_usuario = ""; 

int contSegundos = 0; 

static MediaRecorder recorder; 

Boolean cancelTask = false; 

eChamada chamada; 

Context context; 

public sRecAudioMic() { 
} 



@Override 
public IBinder onBind(Intent intent) { 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    return super.onStartCommand(intent, flags, startId); 
} 

@Override 
public void onCreate() { 
    imei = Functions.getIMEI(sRecAudioMic.this); 

    AUDIO_RECORDER_FOLDER = new _Path().getPathAudio(); 
} 

@SuppressLint("InlinedApi") 
@Override 
public void onStart(Intent intent, int startId) { 

    try 
    { 
     Bundle b = intent.getExtras(); 
     chamada = (eChamada) b.getSerializable("Chamada"); 

     //chamada = (eChamada) intent.getParcelableExtra("Chamada"); 
     String t = ""; 
     t = " "; 
    } 
    catch(Exception e) 
    { 
     String t = ""; 

     t = " "; 
    }  

    String nomeArquivo = ""; 

    int cont = 0; 

    Calendar lCDateTime = Calendar.getInstance(); 

    String t = String.valueOf(lCDateTime.getTimeInMillis()); 

    nomeArquivo = "recording_" + t + FORMATO_AUDIO; 

    nomeArquivo = nomeArquivo.replace(" ", "_").replace(":", "_") 
      .replace("-", "_"); 

    String caminhoArquivo = AUDIO_RECORDER_FOLDER + "/" + nomeArquivo; 

    chamada.nomeArquivo = nomeArquivo; 
    chamada.caminhoArquivo = caminhoArquivo; 

    try { 

     recorder = new MediaRecorder(); 

     try { 

      MediaRecorder r = new MediaRecorder(); 
      r.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 
      r.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
      r.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
      r.setOutputFile(caminhoArquivo); 
      r.setAudioSamplingRate(96000); 
      r.prepare(); 
      r.start(); 
      r.stop(); 
      r.reset(); 
      r.release(); 

      r = null; 

      recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); 

     } catch (Exception e) { 

      MediaRecorder r = new MediaRecorder(); 
      r.setAudioSource(MediaRecorder.AudioSource.MIC); 
      r.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
      r.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
      r.setOutputFile(caminhoArquivo); 
      r.setAudioSamplingRate(96000); 
      r.prepare(); 
      r.start(); 
      r.stop(); 
      r.reset(); 

      r = null; 

      recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     } 

     recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
     recorder.setAudioSamplingRate(96000); 
     // recorder.setMaxDuration(30000); 

     recorder.setOutputFile(caminhoArquivo); 

     recorder.setOnErrorListener(errorListener); 
     recorder.setOnInfoListener(infoListener); 

     recorder.prepare(); 
     recorder.start(); 

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

     new LOG().CriaLog("sRecAudioMic", "onStart()", e.getMessage(), ""); 

    } finally { 

    } 

} 

@Override 
public void onDestroy() { 

    String time = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss") 
      .format(new java.util.Date()); 

    if (null != recorder) { 

     // para gravacao 
     recorder.stop(); 
     recorder.reset(); 
     recorder.release(); 

     recorder = null; 
    } 

    chamada.timestampChamadaTerminada = time; 

    ChamadasDataSource dsCham = new ChamadasDataSource(sRecAudioMic.this); 

    dsCham.open(); 

    dsCham.insert(chamada); 

    dsCham.close(); 

} 

@SuppressLint("NewApi") 
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() { 
    @Override 
    public void onError(MediaRecorder mr, int what, int extra) { 

    } 
}; 

@SuppressLint("NewApi") 
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() { 
    @Override 
    public void onInfo(MediaRecorder mr, int what, int extra) { 

    } 
}; 

}