0

I have the following thread running. I got help from SO, that how to start and stop thread properly.

I am getting the following error in my compiler.

error: exception InterruptedException is never thrown in body of corresponding try statement

I am new to Java world and got stuck.

Just removing the catch block, Is it a proper way to solve the problem?

public class MyThread extends Thread {
  InetAddress inetAddress;
  AudioGroup audioGroup = new AudioGroup();
  MediaPlayer mediaPlayer = new MediaPlayer();

  @Override
  public void run() {
    if (!Thread.interrupted()) {
      try {
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        audioManager.setSpeakerphoneOn(true);

        audioGroup.setMode(AudioGroup.MODE_ECHO_SUPPRESSION);

        mediaPlayer.setAudioStreamType(audioManager.STREAM_VOICE_CALL);

        inetAddress = InetAddress.getByName(getLocalIpAddress());
        audioStream = new AudioStream(inetAddress);
        audioStream.setCodec(AudioCodec.PCMU);
        audioStream.setMode(RtpStream.MODE_NORMAL);
        InetAddress inetAddressRemote = InetAddress.getByName(remoteIp);
        audioStream.associate(inetAddressRemote, remotePort);
        audioStream.join(audioGroup);

        //streaming the audio to speakers
        mediaPlayer.setDataSource(getLocalIpAddress());
        mediaPlayer.prepare();
        mediaPlayer.start();

        //Thread.sleep(4000);
        //throw new InterruptedException("");
      } catch (InterruptedException e) {
        System.out.println("InterruptedException occur" + e.getMessage());
      } catch (UnknownHostException e) {
        e.printStackTrace();
      } catch (SocketException e) {
        e.printStackTrace();
      } catch (IOException IO) {
        IO.printStackTrace();
      }
    }
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
cantona_7
  • 1,127
  • 4
  • 21
  • 40
  • 1
    Just remove the catch for InterruptedException then you'll be fine – Mark Melgo Jul 15 '19 at 07:30
  • Is that simple ? won't I run into any problem ? – cantona_7 Jul 15 '19 at 07:31
  • 1
    Since your code doesn't throw any InterruptedException then you will not run into any problem and besides if in the future your code will throw InterruptedException then the compiler will tell you, so at that time you can add InterruptedException catch block – Mark Melgo Jul 15 '19 at 07:33
  • Removing the catch statement removes the error but thread doesn't seem to stop. It keeps on running even if i stop – cantona_7 Jul 15 '19 at 07:33
  • any reason why you want to do that in a separate thread? Also why do you think that `but thread doesn't seem to stop`?" – Vladyslav Matviienko Jul 15 '19 at 07:49
  • Vladyslav..,I run the thread and connect with the remote server. Once I start the thread `thread.start()`, the connection is established(LED is ON). But if i do `thread.interrupt()` still the connection is existing(Still,LED is ON) – cantona_7 Jul 15 '19 at 07:53
  • 1
    The connection exists until you close the socket. It has nothing to do with the lifetime of the thread. – user207421 Jul 15 '19 at 08:16
  • @user207421 In my case, I should kill the audiostream from the server ? Did I understand correctly? – cantona_7 Jul 15 '19 at 08:19
  • Since `audiostream uses rtp` – cantona_7 Jul 15 '19 at 08:24

0 Answers0