Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Audio Player
    text
    copied!<p>I have and audio recorder and player first i can record audio then the recorded audio can be played ,when audio is playing i want my seekbar thumb move smoothly but currently it have no smooth movement also the fast switching of play and pause sometimes fail my application by saying that illegal state :anybody please suggest a solution for improving my audio player . recding no pblm</p> <pre><code>public class RecorderActivity extends Activity{ public static int AUDIO_RECORDING_RESULT=0; public static int FROM_AUDIO_RECDING_HOME=0; public static int FROM_AUDIO_RECDING_SEARCH=0; public static int FROM_AUDIO_RECDING=0; private static MediaRecorder recorder; private static Button recd_stop_audio; private static Button audio_play_pause; private static Chronometer audio_recding_time; private static Chronometer audio_playtime_begin; private static TextView audio_playtime_end; public static int audio_play_pause_flag = 1; private static int audio_recding_stop_flag = 1; private static SeekBar audio_seekbar_play_indication; private static View v1; private static View v2; private static View v3; private static TextView textview_audio_information_grade; private static TextView textview_audio_information1; private static TextView textview_audio_information2_1; private static TextView textview_audio_information2_2; private static TextView textview_audio_information2; private static TextView textview_audio_information3; private static int audio_seekbar_progress; private static int audio_seekbar_incr; private Button continue_audio; private static Button delete_recding; private static MediaPlayer mp; private static int audio_paused_flag = 0; private static TextView txt_audio_serviceprovidername; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.audio); MyCustomesData.context=RecorderActivity.this; final AlertDialog.Builder builder = new AlertDialog.Builder(RecorderActivity.this); audio_paused_flag=0; FROM_AUDIO_RECDING=0; AUDIO_RECORDING_RESULT=0; MyCustomesData.sumitAReport_PAGE=MyCustomesConstants.USER_AUDIO; continue_audio = (Button) findViewById(R.id.Button_Voice_Record_Continue); textview_audio_information_grade=(TextView)findViewById(R.id.Description_grade); textview_audio_information1 = (TextView) findViewById(R.id.Description); textview_audio_information2 = (TextView) findViewById(R.id.Description2); textview_audio_information2_1=(TextView)findViewById(R.id.Description2_1); textview_audio_information2_2=(TextView)findViewById(R.id.Description2_2); textview_audio_information3= (TextView)findViewById(R.id.Description3); v1 = (View) findViewById(R.id.View1); v2 = (View) findViewById(R.id.View22); v3= (View) findViewById(R.id.View33); audio_seekbar_play_indication = (SeekBar) findViewById(R.id.SeekBar01); audio_playtime_begin = (Chronometer) findViewById(R.id.TimerStart); audio_playtime_end = (TextView) findViewById(R.id.TimerStop); audio_recding_time = (Chronometer) findViewById(R.id.Rec_Timer_ID); recorder = new MediaRecorder(); recd_stop_audio = (Button) findViewById(R.id.Button_record_sound); audio_play_pause = (Button) findViewById(R.id.Button_Play_Sound); delete_recding = (Button) findViewById(R.id.Button_Delete_Sound); txt_audio_serviceprovidername=(TextView)findViewById(R.id.TextView_Audio_Provider); txt_audio_serviceprovidername.setText(MyCustomesData.MyCustomesListContext.getProviderName()); audio_seekbar_play_indication.setOnClickListener(null); audio_seekbar_play_indication.setEnabled(false); audio_play_pause.setEnabled(false); delete_recding.setEnabled(false); showHelpInformation(); if(MyCustomesData.MyCustomesListContext.isAudioRecd()) { showRecdingInformation(); audio_play_pause.setEnabled(true); delete_recding.setEnabled(true); } recd_stop_audio.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (audio_recding_stop_flag == 0) { audiorecding(); } else { audiorecding(); } } }); audio_play_pause.setOnClickListener(new OnClickListener() { public void onClick(View v) { playaudio(); } }); audio_recding_time.setOnChronometerTickListener(new OnChronometerTickListener() { public void onChronometerTick(Chronometer chronometer) { if(chronometer.getText().equals("02:00")) { audiorecding(); } } }); **//UPDATING SEEKBAR USING CHRONOMETER** audio_playtime_begin.setOnChronometerTickListener(new OnChronometerTickListener() { public void onChronometerTick(Chronometer chronometer) { if(!audio_playtime_begin.getText().equals("00:00")) { audio_seekbar_play_indication.setProgress(audio_seekbar_progress); audio_seekbar_progress = audio_seekbar_progress + audio_seekbar_incr; } else audio_seekbar_progress=1; } }); delete_recding.setOnClickListener(new OnClickListener() { public void onClick(View v) { audio_seekbar_progress = 1; if (mp != null) { if (audio_play_pause_flag == 0) { audio_play_pause_flag =1; audio_playtime_begin.stop(); mp.stop(); mp.reset(); mp.release(); audio_play_pause.setBackgroundResource(R.drawable.play1); } } recorder = new MediaRecorder(); v1.setVisibility(View.VISIBLE); v2.setVisibility(View.VISIBLE); recd_stop_audio.setEnabled(true); audio_play_pause.setEnabled(false); delete_recding.setEnabled(false); audio_play_pause.setBackgroundResource(R.drawable.play1); audio_play_pause_flag = 1; audio_playtime_begin.stop(); audio_playtime_begin.setBase(SystemClock.elapsedRealtime()); audio_paused_flag = 0; audio_playtime_end.setText("00:00"); // Seekbar End Time audio_recding_time.setBase(SystemClock.elapsedRealtime()); // Recording time audio_seekbar_play_indication.setProgress(0); MyCustomesData.MyCustomesListContext.setAudioRecd(false); showHelpInformation(); } }); continue_audio.setOnClickListener(new OnClickListener() { public void onClick(View v) { System.out.println("The Staus of report"+MyCustomesData.MyCustomesListContext.isAudioRecd()+":"+MyCustomesData.MyCustomesListContext.getTextReport()); if (audio_recding_stop_flag == 0) { audiorecding(); } if(audio_play_pause_flag == 0) { mp.stop(); mp.reset(); mp.release(); audio_seekbar_play_indication.setProgress(100); //Max value audio_playtime_begin.stop(); audio_play_pause_flag = 1; audio_play_pause.setBackgroundResource(R.drawable.play1); } StringBuffer txtReport=new StringBuffer(MyCustomesData.MyCustomesListContext.getTextReport()); String txtreport=new String(txtReport); txtreport=txtreport.trim(); if(!MyCustomesData.MyCustomesListContext.isAudioRecd()&amp;&amp;txtreport.length()==0) { builder.setMessage("xxxxxxxxxxxxxxxx.").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); FROM_AUDIO_RECDING=0; AUDIO_RECORDING_RESULT=0; finish(); } }); AlertDialog alert = builder.create(); alert.setTitle("Attention"); alert.setIcon(R.drawable.alert_dialog_icon); alert.show(); } else { AUDIO_RECORDING_RESULT=1; finish(); } } }); } static void audiorecding() { audio_play_pause.setEnabled(false); delete_recding.setEnabled(false); if (audio_recding_stop_flag == 0) { int maxtime = audio_seekbar_play_indication.getMax(); audio_recding_stop_flag = 1; audio_recding_time.stop(); recorder.stop(); recorder.reset(); recorder.release(); audio_seekbar_progress = 1; recd_stop_audio.setBackgroundResource(R.drawable.rec_btn); audio_play_pause.setEnabled(true); delete_recding.setEnabled(true); MyCustomesData.MyCustomesListContext.setAudioRecd(true); if(audio_recding_time.getText().equals("00:00")) { audio_recding_time.setText("00:01"); MyCustomesData.MyCustomesListContext.setAudioRecdingTime( "00:01"); } else MyCustomesData.MyCustomesListContext.setAudioRecdingTime( audio_recding_time.getText().toString()); showRecdingInformation(); } else { audio_recding_stop_flag = 0; try { showHelpInformation(); audio_playtime_begin.setBase(SystemClock.elapsedRealtime()); audio_playtime_end.setText("00:00"); audio_seekbar_play_indication.setProgress(0); MyCustomesListMain.path = InitialValueLoader.sanitizePath("MyCustomAudio/VoiceReport"); InitialValueLoader.filecheck(MyCustomesListMain.path); audio_play_pause.setEnabled(false); delete_recding.setEnabled(false); recd_stop_audio.setBackgroundResource(R.drawable.recording_stop); audio_recding_time.setBase(SystemClock.elapsedRealtime()); recorder.release(); audio_recding_time.start(); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // AudioSource recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // OutputFormat recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); recorder.setOutputFile(MyCustomesListMain.path); recorder.prepare(); recorder.start(); } catch (Exception e) { e.printStackTrace(); recorder.reset(); recorder.release(); } } } private static void playaudio() { showRecdingInformation(); recd_stop_audio.setEnabled(false); if (audio_play_pause_flag == 1) { audio_play_pause_flag = 0; v2.setVisibility(View.GONE); audio_playtime_end.setText(audio_recding_time.getText()); audio_play_pause.setBackgroundResource(R.drawable.play2); if (audio_paused_flag == 1) { audio_paused_flag = 0; int stoppedMilliseconds = 0; String chronoText = audio_playtime_begin.getText().toString(); String array[] = chronoText.split(":"); if (array.length == 2) { stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000 + Integer.parseInt(array[1]) * 1000; } else if (array.length == 3) { stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000 + Integer.parseInt(array[1]) * 60 * 1000 + Integer.parseInt(array[2]) * 1000; } stoppedMilliseconds=stoppedMilliseconds+1000; audio_playtime_begin.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds); audio_seekbar_progress=audio_seekbar_progress - 1; audio_playtime_begin.start(); mp.start(); } else { v2.setVisibility(View.GONE); try { audio_play_pause.setBackgroundResource(R.drawable.play2); audio_seekbar_progress = 1; audio_seekbar_play_indication.setMax(getTotalAudioRecdingTime(MyCustomesData.MyCustomesListContext.getAudioRecdingTime())); audio_play_pause.setBackgroundResource(R.drawable.play2); audio_playtime_end.setText(MyCustomesData.MyCustomesListContext.getAudioRecdingTime()); audio_seekbar_incr = 1; audio_playtime_begin.setBase(SystemClock.elapsedRealtime()); audio_seekbar_play_indication.setProgress(0); audio_playtime_begin.start(); mp = new MediaPlayer(); mp.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer mp) { mp.stop(); mp.reset(); mp.release(); audio_playtime_begin.stop(); audio_seekbar_play_indication.setProgress(0); audio_playtime_begin.setText(MyCustomesData.MyCustomesListContext.getAudioRecdingTime()); audio_seekbar_play_indication.setProgress(getTotalAudioRecdingTime(MyCustomesData.MyCustomesListContext.getAudioRecdingTime())); //Max value audio_play_pause_flag = 1; audio_play_pause.setBackgroundResource(R.drawable.play1); recd_stop_audio.setEnabled(true); } }); mp.setDataSource(MyCustomesListMain.path); mp.prepare(); mp.start(); } catch (Exception e) { e.printStackTrace(); mp.stop(); mp.reset(); mp.release(); audio_playtime_begin.stop(); } } } else { System.out.println("Audio Palyer Paused&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;"); audio_play_pause_flag = 1; audio_paused_flag = 1; audio_play_pause.setBackgroundResource(R.drawable.play1); audio_playtime_begin.stop(); mp.pause(); } } private static int getTotalAudioRecdingTime(String recdingtime) { int timeM=Integer.valueOf(recdingtime.substring(0, 2)); System.out.println("The Minute"+timeM); int timeS=Integer.valueOf(recdingtime.substring(3, 5)); int totaltimeinS=timeM*60+timeS; return totaltimeinS; } private static void showHelpInformation() { } private static void showRecdingInformation() { } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload