I have do async method which is getting sound stream from ip from specific port. But some times it is not recognize by application if stream port is closed or not, here is code:
protected Integer doInBackground(String... params) {
// define data byte
byte[] data= new byte[400];
try {
// boolean for loop
looping = true;
// create new socket
socket = new Socket(params[0], Integer.parseInt(params[1]));
// getting socket time out
int time = socket.getSoTimeout();
// closing delay of tcp
socket.setTcpNoDelay(true);
// checking for looping
publishProgress();
// problem is here, some times looping is not recognize when socket closed
// here is test:
// when stream started then connection closed,
// socket connection doesn't recognize if connection closed or not even if looping is false
// loop doesnt break
while (looping) {
if (!socket.isConnected()) { // if socket not connected close
// if connection closed publish next progress to onProgressUpdate
publishProgress();
// setting looping boolean to false and breake while loop
looping= false;
} else { // if socket connected
// confirmation integer
int onay = -1;
try {
// setting socket time out
socket.setSoTimeout(100); // setting socket time out
// defining input stream for media player
InputStream is = socket.getInputStream();
onay = is.read(data); // if read data is possible set onay integer
if (my.mp.isPlaying()) { // if media player playing
my.mp.pause(); // pause it
}
} catch (SocketTimeoutException en) {
// setting socket time out
socket.setSoTimeout(time);
}
// write data to track
if (onay != -1) {
// if confirmation onay not -1 write data to track
my.annocuncementAudioTrack.write(data, 0, data.length);
}
OutputStream out = socket.getOutputStream(); // getting stream from socket
out.write(0); // write to outputstream file
}
}
} catch (IOException e) {
// return one in case of any error
return 1;
}
// return zero when operation finish
return 0;
}
Problem is even looping boolean changed to false while loop is not breaking, why is that. thank you.