媒体录制器服务中的奇怪行为

时间:2016-01-24 03:07:00

标签: android android-service android-mediaplayer

我正在开始使用鼠标录制语音服务。它成功运行,因为它在OnCreate服务中显示通知和Toast。但是控件永远不会出现在mediaRecorder.prepare尝试块上,它甚至不会在catch块中显示我的吐司。 这是我的AudioRecorder服务

public class ServiceBgAudioRecorder extends Service {
    public static boolean isRecording = false;
    public static MediaRecorder mediaRecorder;

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

    @Override
    public void onCreate() {
        Toast.makeText(this,"Oncreate of service",Toast.LENGTH_LONG).show();
        startRecording();
    }
    private void startRecording() {
        Notification notification = new Notification.Builder(this)
                .setContentTitle("Background Audio Recorder" )
                .setContentText("Audio recording is started")
                .setSmallIcon(R.drawable.appicon)
                .build();
        startForeground(1234, notification);
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFile(AppUtilities.getOutputMediaFile(AppUtilities.MEDIA_TYPE_3GP).getAbsolutePath());
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            Toast.makeText(this,"Audio Recording is started",Toast.LENGTH_LONG);
            isRecording = true;
        } catch (IOException e) {
            Toast.makeText(this,"Can't start Audio Recording",Toast.LENGTH_LONG);
        }
    }
    private void stopRecording() {
        mediaRecorder.release();
        mediaRecorder = null;
        isRecording = false;
    }

    @Override
    public void onDestroy() {
        stopRecording();
    }
}

我不知道为什么控件没有到达try / catch块。

1 个答案:

答案 0 :(得分:1)

你需要展示吐司吗? 将show()方法附加到 makeText() Toast.makeText(getActivity(),“音频录制已开始”,Toast.LENGTH_LONG)。show();