0

I have created app that will send audio streaming to server when user click on start button.For that i created a socket thread to send audio to server.And it stop after pressing the stop button.It works perfect,But the problem is that when i start streaming and navigate from current activity to different activity by pressing the back button or from action bar menu and come on home screen to stop the streaming am not able to stop the streaming.It means the thread is running at background.

So my question is how can i stop that thread or streaming.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Avinash Aher
  • 77
  • 2
  • 10
  • 1
    You should post your code here, so that others could see and help. – gprathour Jan 09 '15 at 07:33
  • @GPRathour - in a word, NO. The poster has described the operation of their code in a way that is a far more useful than having a page of it dumped on us, and entirely sufficient for the present purposes. – Chris Stratton Jan 19 '15 at 05:11

3 Answers3

0

Try to close the socket before you navigate to a different activity.

roman
  • 66
  • 1
  • 7
0

override the onPause() method and close the socket there.

PPD
  • 5,660
  • 12
  • 52
  • 86
0

A fairly clean solution would be to have a volatile flag shared between your threads. Your Audio streaming thread should check this each time it handles a block of data, closing the connection and stopping if it has been set to false. Your UI thread can set it to false when onPause() is called - or possibly onStop() might be a better fit, as that indicates when your Activity is no longer visible.

This may mean that your streaming thread hangs around for a fraction of a second and gets one more tiny bit off audio sent off, but that is unlikely to be an issue in practice.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117