media player - Android MediaRecorder recording looping back on itself -


i have made app uses mediarecorder , mediaplayer, whenever record , play it, kinda loops on itself.

e.g say, one, two, three, four, five, six, seven, eight, nine, ten

but plays one, two, 3 three, 4 four, 5 five 5 etc. gets worse if recording longer?

it might simple bug, don't see it.

here code :

import android.media.audiomanager; import android.media.mediaplayer; import android.media.mediarecorder; import android.net.uri; import android.os.countdowntimer; import android.os.environment; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.compoundbutton; import android.widget.progressbar; import android.widget.switch; import android.widget.toast;  import java.io.file; import java.io.ioexception;  public class mainactivity extends appcompatactivity {  switch switchplay; button btnrecord, btndelete; progressbar pbarmetronome;  mediarecorder mrecorder1, mrecorder2, mrecorder3, mrecorder4, mrecorder5, mrecorder6; mediaplayer mp1, mp2, mp3, mp4, mp5, mp6;  string sdpath = "/sdcard/looper recordings/";  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       switchplay = (switch)findviewbyid(r.id.switchplay);     btnrecord = (button)findviewbyid(r.id.buttonrecord);     btndelete = (button)findviewbyid(r.id.buttondelete);      pbarmetronome = (progressbar)findviewbyid(r.id.progressbarmetronome);      clearfile();     createfile();     lineone(); }  private void playone(){     mp1 = new mediaplayer();     uri myuri = uri.parse("/sdcard/looper recordings/one.mp3");     mp1.setaudiostreamtype(audiomanager.stream_music);     try {         mp1.setdatasource(getapplicationcontext(), myuri);     } catch (ioexception e) {         e.printstacktrace();     }     try {         mp1.prepare();         toast.maketext(mainactivity.this, "preparing", toast.length_short).show();     } catch (ioexception e) {         e.printstacktrace();     }      mp1.start();     toast.maketext(mainactivity.this, "play!", toast.length_short).show(); }  private void recordone(){     mrecorder1 = new mediarecorder();     mrecorder1.setaudiosource(mediarecorder.audiosource.mic);     mrecorder1.setoutputformat(mediarecorder.outputformat.mpeg_4);     mrecorder1.setaudioencoder(mediarecorder.audioencoder.aac);     mrecorder1.setoutputfile(sdpath+"one.mp3");     //mrecorder1.setaudioencoder(mediarecorder.getaudiosourcemax());     mrecorder1.setaudioencodingbitrate(16);     mrecorder1.setaudiosamplingrate(44100);     try {         mrecorder1.prepare();         toast.maketext(mainactivity.this, "preparing...", toast.length_short).show();     } catch (ioexception e) {         e.printstacktrace();     }             toast.maketext(mainactivity.this, "starting...", toast.length_short).show();             mrecorder1.start();              new countdowntimer(5000,1000){                 @override                 public void ontick(long millisuntilfinished) {                  }                  @override                 public void onfinish() {                     mrecorder1.stop();                     toast.maketext(mainactivity.this, "stopped!", toast.length_short).show();                 }             }.start(); }  private void lineone(){     btndelete.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             recordone();          }     });     btnrecord.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {              new countdowntimer(4000,1000){                 @override                 public void ontick(long millisuntilfinished) {                     btnrecord.settext(""+(millisuntilfinished/1000));                     playone();                 }                  @override                 public void onfinish() {                  }             }.start();          }     });     switchplay.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {         @override         public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {             if (ischecked){                 //on                }else{                 //off               }         }     }); }  private void clearfile(){     file dir = new file(environment.getexternalstoragedirectory()+"/sdcard/looper recordings");     if (dir.isdirectory())     {         string[] children = dir.list();         (int = 0; < children.length; i++)         {             new file(dir, children[i]).delete();         }     } }  private void createfile(){     file recordingdirectory = new file(environment.getexternalstoragedirectory()+ file.separator+"looper recordings");      if(!recordingdirectory.exists() && !recordingdirectory.isdirectory())     {         // create empty directory         if (recordingdirectory.mkdirs())         {             toast.maketext(mainactivity.this, "folder recordings created!", toast.length_short).show();         }         else         {             toast.maketext(mainactivity.this, "folder recordings not created?", toast.length_short).show();         }     }     else     {     } } } 

i know it's messy i'm still prototyping

thanks willing help!

it seems initializing mediaplayer multiple times , play them simeltenously


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -