audio - getDuration() gives 0 output for one file in Android App -
my app loads 2 audio files database , stores them in array. user can play of them selecting 1 radio button group. both mp3. 1 playing fine , it's elapsed , total duration displaying correctly. same functions display 00:00 total duration other. seek bar updates progress 100% in case elapsed time correctly displaying , audio playing fine. please tell problem? why happening.. , how can resolve it??
audio_urdu
's time fine.. error audio_eng
.
private void updateview(int i) throws jsonexception { idx=0; _imgbtnplay.setclickable(false); _imgbtnstop.setclickable(false); jsonobject jobject=null; jobject=jarray.getjsonobject(i); audiourl_eng=jobject.getstring("audio_eng"); audiourl_urdu=jobject.getstring("audio_urdu"); lbl_tduration.settext("00:00"); lbl_cduration.settext("00:00"); lbl_loading.settext("loading audio files..."); loadaudio(audiourl_eng); } // loading audio files url private void loadaudio(string url) { // todo auto-generated method stub mmediaplayer=new mediaplayer(); mmediaplayer.setaudiostreamtype(audiomanager.stream_music); try { mmediaplayer.setdatasource(url); mmediaplayer.prepareasync(); } catch(ioexception e) { e.printstacktrace(); } catch (illegalargumentexception e) { e.printstacktrace(); } catch (illegalstateexception e) { e.printstacktrace(); } mmediaplayer.setonpreparedlistener(this); } // notify when audio ready played @override public void onprepared(mediaplayer mp) { // todo auto-generated method stub audioplayerlist[idx]=mp; idx++; if (idx == 1) { audioplayer = mp; lbl_tduration.settext(msectotimer(mp.getduration())); lbl_cduration.settext(msectotimer(mp.getcurrentposition())); updateseekbar(); loadaudio(audiourl_eng); } if (idx == 2) { // enabling media control buttons _imgbtnplay.setclickable(true); _imgbtnstop.setclickable(true); rdgrp.setclickable(true); lbl_loading.settext(""); idx = 0; } } public void onclick(view v) { switch(v.getid()) { // calling search function case r.id.imgbtnsearch: onsearchrequested(); break; // when play/pause button tapped case r.id.imgbtnplay: if (audioplayer.isplaying()) { audioplayer.pause(); _imgbtnplay.setimageresource(r.drawable.ic_action_play); } else if (audioplayer!=null) { audioplayer.start(); _imgbtnplay.setimageresource(r.drawable.ic_action_pause); durationhandler.postdelayed(updateseekbartime, 100); } break; // when stop button tapped case r.id.imgbtnstop: audioplayer.pause(); _imgbtnplay.setimageresource(r.drawable.ic_action_play); resetprogress(); break; default: break; } // updating seek bar's time after every 100 milliseconds public void updateseekbar() { durationhandler.postdelayed(updateseekbartime, 100); } // updating progress of seek bar private runnable updateseekbartime = new runnable() { public void run() { long tduration = audioplayer.getduration(); long cduration = audioplayer.getcurrentposition(); lbl_tduration.settext(msectotimer(tduration)); lbl_cduration.settext(msectotimer(cduration)); int progress = (int) getprogresspercentage(cduration, tduration); _seekbar.setprogress(progress); durationhandler.postdelayed(this, 100); } }; // converting milliseconds min:sec format public string msectotimer(long ms) { string finaltimerstring = ""; string secstring = ""; string minstring = ""; // convert total duration minutes , seconds int min = (int)(ms % (1000*60*60)) / (1000*60); int sec = (int) ((ms % (1000*60*60)) % (1000*60) / 1000); // prepending 0 seconds if 1 digit if(sec < 10) secstring = "0" + sec; else secstring = "" + sec; // prepending 0 minutes if 1 digit if(min < 10) minstring = "0" + min; else minstring = "" + min; finaltimerstring = minstring + ":" + secstring; return finaltimerstring; } // calculating percentage progress of seek bar public int getprogresspercentage(long cduration, long tduration) { double percentage = (double) 0; long cseconds = (int) (cduration / 1000); long tseconds = (int) (tduration / 1000); percentage =(((double)cseconds)/tseconds)*100; return percentage.intvalue(); } // converting progress of seek bar time duration in milliseconds public int progresstotimer(int progress, int tduration) { int cduration = 0; tduration = (int) (tduration / 1000); cduration = (int) ((((double)progress) / 100) * tduration); return cduration * 1000; } // reseting progress of seek bar when stop button tapped public void resetprogress() { audioplayer.seekto(0); lbl_cduration.settext(msectotimer(0)); _seekbar.setprogress(0); }
i have 1 audio in english , 1 in urdu language. both in array audioplayerlist
. user can select different languages using radio buttons. , idx
variable tells audio file played. audio_eng
on index 0 (idx = 0
) , audio_urdu
on index 1 (idx = 1
). audio selected audioplayer = audioplayerlist[idx]
code radio button selection this:
rdgrp.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(radiogroup group, int checkedid) { // find radio button selected if (audioplayer!=null) { if(audioplayer.isplaying()) audioplayer.pause(); _imgbtnplay.setimageresource(r.drawable.ic_action_play); resetprogress(); if (checkedid == r.id.rdeng) audioplayer = audioplayerlist[0]; else if (checkedid == r.id.rdurdu) audioplayer = audioplayerlist[1]; } } });
duration of file 'audio_eng' might less 1 second. when calculating percentage convert milliseconds seconds results in 0
total length. that's why progress bar set 100% beginning (actually, exception might thrown in case - did check that?).
when calculatng percentage
try not convert xduration
into xseconds
divide durations in method getprogresspercentage
.
i couldn't find other reasons why such result
Comments
Post a Comment