我正在Android中制作一个电话录音机应用程序。我有一个检测来电和去电的服务,一旦服务检测到来电或去电,就会通过媒体播放器开始录音。它已成功录制,但我无法在扬声器上播放。即使通过音频管理器启用扬声器后,音量也非常低。请告诉我可能出错的地方。
注意:我猜测我的音频没有被正确录制,因为如果我通过手机音乐播放器播放录制的文件,那么即使在那里我也无法在扬声器上播放该文件。
下面的是用于开始录制的代码:
try{
MediaRecorder mediaRecorder = new MediaRecorder();
if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_COMMUNICATION"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_UPLINK"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_DOWNLINK"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("MIC"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("DEFAULT"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
}
else
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(getTempFilename());
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaRecorder.start();
以下是媒体播放代码:
final MediaPlayer mp=new MediaPlayer();
String uri= getIntent().getStringExtra("fileURI"); //fetches the string defining the path of file to be played
Uri fileURI = Uri.parse(uri);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setSpeakerphoneOn(true);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);
try{
try {
mp.setDataSource(getApplicationContext(), fileURI);
} catch (IOException e) {
e.printStackTrace();
}
}
mp.prepare();
}catch(Exception e){e.printStackTrace();}
mp.start();