Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果在单击“开始”按钮之前先单击“停止记录器”按钮,则单击该按钮时会引发空异常_Android_Mediarecorder - Fatal编程技术网

Android 如果在单击“开始”按钮之前先单击“停止记录器”按钮,则单击该按钮时会引发空异常

Android 如果在单击“开始”按钮之前先单击“停止记录器”按钮,则单击该按钮时会引发空异常,android,mediarecorder,Android,Mediarecorder,我使用这段代码已经有一段时间了,直到我发现在单击startButton之前第一次单击Stop recorderButton时抛出null异常。 我把录音机放在弹出窗口里。所以,如果我打开应用程序并决定先单击stopRecording,它将抛出异常 但如果我先点击startRecording,然后点击stopRecorder,一切都会顺利进行 这是我的RecorderActivity.java //Init View btnPlay = (Button)findViewById(R

我使用这段代码已经有一段时间了,直到我发现在单击startButton之前第一次单击Stop recorderButton时抛出null异常。 我把录音机放在弹出窗口里。所以,如果我打开应用程序并决定先单击stopRecording,它将抛出异常

但如果我先点击startRecording,然后点击stopRecorder,一切都会顺利进行

这是我的RecorderActivity.java

//Init View
        btnPlay = (Button)findViewById(R.id.btnPlay);
        btnStop = (Button) findViewById(R.id.btnStop);
        btnStartRecorder = (Button) findViewById(R.id.startRecord);
        btnStopRecorder = (Button) findViewById(R.id.stopRecord);
        //implementing the actions
        btnStartRecorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkPermissionFromDevice())
                {


                    Intent i = new Intent(getApplicationContext(), MyService.class);
                    i.setAction("C.ACTION_START_SERVICE");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        startForegroundService(i);
                    }
                    else
                    {
                        startService(i);

                    }

                    btnPlay.setEnabled(false);
                    btnStop.setEnabled(false);
                    btnStopRecorder.setEnabled(true);

                    Toast.makeText(RecorderActivity.this, "Recording...", Toast.LENGTH_SHORT).show();
                }

                else {
                    requestPermission();
                }

            }
        });

        btnStopRecorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyService.mediaRecorder.stop();
                pathSave = MyService.pathSave;
                stopService(new Intent(RecorderActivity.this, MyService.class));
                btnStopRecorder.setEnabled(false);
                btnPlay.setEnabled(true);
                btnStartRecorder.setEnabled(true);
                btnStop.setEnabled(false);
                Toast.makeText(RecorderActivity.this, "Stop Record...", Toast.LENGTH_SHORT).show();
            }
        });
这是录音机的MyService.java

public class MyService extends Service {
    static MediaRecorder mediaRecorder;
    static final int NOTIFICATION_ID = 543;
    static String pathSave = "";

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(1, new Notification());

        pathSave = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/"
                + UUID.randomUUID().toString() + "_audio_record.amr";
        setupMediaRecorder(); // add this line in your service
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            Toast.makeText(getApplicationContext(), "Recording...", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @RequiresApi(Build.VERSION_CODES.O)
    private void startMyOwnForeground() {

        String NOTIFICATION_CHANNEL_ID = "example.permanence";
        String channelName = "Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);


        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setContentTitle("App is running in background")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)

                .build();
        startForeground(2, notification);
    }

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

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


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    //add this function in your service
    private void setupMediaRecorder() {
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(pathSave);

    }

}

当您单击停止按钮时,您的MyService尚未运行。所以它试图阻止一些不存在的东西

选项1:禁用停止按钮,直到服务启动


选项2:检查服务是否在停止按钮onclick方法中运行。如果运行停止它,如果不做什么。下面是另一个答案,它显示了如何查看服务是否正在运行

这表明你正确理解了我的问题。如果我将这个android:enabled=“false”放在xml按钮中,它会工作,但这里的问题是:如果应用程序正在录制,我关闭popmenu播放我想要录制的活动,然后返回popmenu,按钮的状态将变为禁用,使我无法停止录制。除了我触摸开始,然后在录音最终停止前的一瞬间停止。在这种情况下,我该怎么办?这会让新用户感到困惑,他们不知道该做什么,录音机会在用户不知情的情况下继续录音。这就是我设置android:enabled=“true”的时候,这样我就可以找到一些解决方法。感谢您的帮助。改善用户体验的另一个选择是通过添加在服务运行期间显示的通知,使您的服务成为粘性服务。然后,他们将始终能够看到它,如果他们意外地启动了它,然后离开了您的应用程序,通知将仍然存在,单击它应该会使他们返回开始-停止活动。