質問

I want to record streaming radio

now this is my code

press.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {



            String filename = Environment.getExternalStorageDirectory().getAbsolutePath();
             filename += "/music.mp3";

             outputSource= new File(filename);
             bytesRead = -1;


            URL url;
            try {
                url = new URL("** URL **");
                 inputStream = url.openStream();
                    Log.d(LOG_TAG, "url.openStream()");

                    fileOutputStream = new FileOutputStream(outputSource);
                    Log.d(LOG_TAG, "FileOutputStream: " + outputSource);
                    while ((c = inputStream.read()) != -1) {
                        fileOutputStream.write(c);
                        bytesRead++;
                    }

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    });

when i click the button it run continuously and can't be stopped, when i click the stop button it said to me that the program is not responding i have to wait or exit

so how i can start the record when i click record button and stop it when i click stop button ?

役に立ちましたか?

解決

You do network operations on UI thread. This is not only wrong, this will throw NetworkOnMainThreadException

Use AsyncTask or create new Thread, which will be terminated on cancel.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top