Who can help stop the flow?
to method: void getTrackMetaData(String mLinkToChannel)
transmitted (from StationSwitcher
) link to the online stream (radio station). This method makes a request every 3 seconds and pulls out from the stream data about the song that is playing. Moreover, the TextView
with the track information is immediately updated in the same method: titleTextView.post(...)
Accordingly, if you turn on one station and do not switch, then everything works well). The TextView
displays, and if the track changes, the track information is updated. However, if you switch the station, i.e. run the method again (from StationSwitcher
) with the new link, then the old stream works (with the old link) and a new one is created.
The question is how to stop the old flow?
private void getTrackMetaData(String mLinkToChannel) { final Utils.TrackData[] trackData = new Utils.TrackData[1]; Thread thread = new Thread(new Runnable() { @Override public void run() { while (!Thread.currentThread().isInterrupted()) { try { Thread.sleep(3000); try { URL url = new URL(mLinkToChannel) ParsingHeaderData streaming = new ParsingHeaderData(); trackData[0] = streaming.getTrackDetails(url); artistTextView.post(new Runnable() { @Override public void run() { artistTextView.setText( ((trackData[0].artist).equals("")) ? mTitleGag : trackData[0].artist); } }); titleTextView.post(new Runnable() { @Override public void run() { titleTextView.setText( ((trackData[0].title).equals("")) ? " " : trackData[0].title); } }); } catch (Exception e) {e.printStackTrace();} } catch (InterruptedException x) {e.printStackTrace(); } catch (Exception e) {e.printStackTrace();} } } }); thread.start(); thread.interrupt(); }
p / s
Utils.TrackData is a class in which two String fields: artist
and title
.
ParsingHeaderData is a class that climbs into a stream and pulls out an object of type Utils.TrackData
.