如何使用 OnClick 事件关闭线程中的 inputStream 和 fileOutputstream 运行

How to close inputStream and fileOutputstream running in a thread by using OnClick event

我录制了一个来自 mPlayer 的流,但找不到如何停止录制 onClick 的方法?应该如何看待 stopRecord 功能? 有什么建议吗?

这是我的代码。

        recordToggle.setOnClickListener(new View.OnClickListener() {
        Thread recordingThread;
        InputStream inputStream = null;
        FileOutputStream bufferedOutputStream = null;

        private void testMediaBuffer() {

记录应该 运行 在单独的线程中,但 Thread.stop 已被弃用

            recordingThread = new Thread(new Runnable() {
                public void run() {

                    String outputSource = Environment.getExternalStorageDirectory().getAbsolutePath() + folder + "/my.mp3";
                    try {
                        fos = new FileOutputStream(outputSource);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    int c;
                    try {


                        URL url = new URL(mPlayer.getDataSource());
                            inputStream = url.openStream();

                            HttpURLConnection con = (HttpURLConnection) url.openConnection();
                            con.setRequestMethod("GET");
                            con.connect();
                            int status = con.getResponseCode();
                            Log.d(TAG, String.valueOf(status));
                            InputStream is = con.getInputStream();
                            int bytesRead = 0;
                            byte[] buf = new byte[1024];
                            try {
                                while ((c = is.read(buf)) != -1) {
                                        Log.d(TAG, " bytesRead=" + bytesRead + " buf: " + Arrays.toString(buf));
                                        try {
                                            fos.write(buf, 0, c);
                                        } catch (IllegalThreadStateException | IOException e) {
                                            e.printStackTrace();
                                        }
                                    fos.flush();
                                    bytesRead++;
                                }is.close();
                            fos.close();
                        } catch (NullPointerException | SocketException e) {
                            e.printStackTrace();
                        }

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

            recordingThread.start();
        }

        //-- runnable

        private void stopRecord() throws IOException {
            //out.close();
            inputStream.close();
            bufferedOutputStream.close();
            bufferedOutputStream.flush();

        }

        @Override
        public void onClick(View v) { //REC
            if (recordToggle.isEnabled()) {

                if (mPlayer.isPlaying()) {

                    getAlbumStorageDir();
                    testMediaBuffer(); 



                    play.setEnabled(false);

                } else {

                    play.setEnabled(true);
                }
            } else {

                play.setEnabled(true);
                try {
                    stopRecord();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


        }

    });
boolean isRunning = true;


recordingThread = new Thread(new Runnable() {
            public void run() {

                String outputSource = Environment.getExternalStorageDirectory().getAbsolutePath() + folder + "/my.mp3";
                try {
                    fos = new FileOutputStream(outputSource);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                int c;
                try {


                    URL url = new URL(mPlayer.getDataSource());
                        inputStream = url.openStream();

                        HttpURLConnection con = (HttpURLConnection) url.openConnection();
                        con.setRequestMethod("GET");
                        con.connect();
                        int status = con.getResponseCode();
                        Log.d(TAG, String.valueOf(status));
                        InputStream is = con.getInputStream();
                        int bytesRead = 0;
                        byte[] buf = new byte[1024];
                        try {
                            while (isRunning && (c = is.read(buf)) != -1) {
                                    Log.d(TAG, " bytesRead=" + bytesRead + " buf: " + Arrays.toString(buf));
                                    try {
                                        fos.write(buf, 0, c);
                                    } catch (IllegalThreadStateException | IOException e) {
                                        e.printStackTrace();
                                    }
                                fos.flush();
                                bytesRead++;
                            }is.close();
                        fos.close();
                    } catch (NullPointerException | SocketException e) {
                        e.printStackTrace();
                    }

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

        recordingThread.start();
    }


  public void stopRecording() {
     isRunning = false;
  }

这是一个示例代码。 解释: 一个全局变量布尔值,表示您的重新编码,而它是 True。 当你想停止记录时,将变量设置为 false。 这将离开 while 循环。 继续 Thread 中的其余代码,并在 运行() 中的代码结束时关闭线程。 在 while 循环之后,在您进入线程时关闭 Streams。