java - what is a good way to stop a thread in android -


i trying passthrough input obtained microphone speaker (the goal able perform audio processing in real time in future). code:

public class mainactivity extends activity {     audiomanager = null;     audiorecord record =null;     audiotrack track =null;     final int sample_frequency = 44100;     final int size_of_record_array = 1024;  // 1024 original     final int wav_sample_multiplication_factor = 1;     int i= 0;     boolean isplaying = true;     class mythread extends thread{         @override         public void run(){             recordandplay();         }     }      mythread newthread;      private void init() {         int min = audiorecord.getminbuffersize(sample_frequency, audioformat.channel_in_mono, audioformat.encoding_pcm_16bit);         record = new audiorecord(mediarecorder.audiosource.voice_communication, sample_frequency, audioformat.channel_in_mono,                                  audioformat.encoding_pcm_16bit, min);         int maxjitter = audiotrack.getminbuffersize(sample_frequency, audioformat.channel_out_mono, audioformat.encoding_pcm_16bit);         track = new audiotrack(audiomanager.mode_in_communication, sample_frequency, audioformat.channel_out_mono,                                audioformat.encoding_pcm_16bit, maxjitter, audiotrack.mode_stream);      }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         setvolumecontrolstream(audiomanager.mode_in_communication);         init();         newthread = new mythread();         newthread.start();     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      private void recordandplay() {         short[] lin = new short[size_of_record_array];         int num = 0;         = (audiomanager) this.getsystemservice(context.audio_service);         am.setmode(audiomanager.mode_in_communication);         record.startrecording();         track.play();         while (true) {             num = record.read(lin, 0, size_of_record_array);             for(i=0;i<lin.length;i++)                 lin[i] *= wav_sample_multiplication_factor;              track.write(lin, 0, num);         }     }      public void passstop(view view){         button playbtn = (button) findviewbyid(r.id.playbtn);           // /*         if(!isplaying){             record.startrecording();             track.play();             isplaying = true;             playbtn.settext("pause");         }         else{            record.stop();            track.pause();            isplaying=false;            playbtn.settext("pass through");         }         // */     }  /*     @suppresswarnings("deprecation")     @override     public void ondestroy(){         newthread.stop();     }     */      @override     protected void ondestroy() {         android.os.process.killprocess(android.os.process.mypid());         // killprocess(android.os.process.mypid());     } }   

brief overview:
while(true) {} infinite loop in recordandplay() function continuously reads raw audio samples microphone , outputs raw samples speaker. recordandplay() called thread started in oncreate() function. starts sending input on microphone speaker program starts (well after few seconds lag think latency in unavoidable). have button can pause , resume pass through. if thread not stopped, pass through continues when exit application or application looses focus (so when phone on desktop keeps doing passthrough).

@suppresswarnings("deprecation") @override public void ondestroy(){     newthread.stop(); }   

this code causes app crash on exit (why?) used

@override protected void ondestroy() {     android.os.process.killprocess(android.os.process.mypid());     // killprocess(android.os.process.mypid()); }   

that found somewhere in stackoverflow (i forgot where). seems want now, want know if proper way stop thread or not. need, is, stops passthrough when exit application, not sure killprocess() function application overall, , if best way stop thread started myself.

also, can same effect if exit application (or loose focus it) while passthrough being paused. assume means thread still running means infinite loop continuously running well. idea this, is, leave thread running, long overall program behaving want to? if have lots of threads or other background processes running? can practice cause memory problems in future if app grows big?

threads should periodically check shouldterminate flag in loop, set flag ui thread , (optionally) wait until thread terminate gracefully. don't forget volatile or proper field synchronization.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -