I can not understand how to free up the memory from MediaPlayer when you click onBackpressed if the player plays
import java.io.file;
Import java.io.fileoutputstream;
Import java.io.ioException;
Import java.io.inputstream;
Import java.net.httpurlConnection;
Import java.net.malformedurlexception;
Import java.net.url;
Import Android.app.ActiveIVITY;
Import android.app.progressDialog;
Import android.content.context;
Import Android.Content.intent;
Import Android.media.AudioManager;
Import Android.media.mediaPlayer;
Import Android.os.asynctask;
Import android.os.Bundle;
Import android.os.Environment;
Import Android.os.Handler;
Import android.util.Base64;
Import Android.View.View;
Import android.view.view.onclicklistener;
Import android.widget.button;
Import android.widget.seekbar;
Import android.widget.textView;
Import android.media.mediaplayer.onbufferingUpdatelistener;
Import android.media.mediaplayer.oncompletionlistener;
Import android.view.motionEvent;
Import android.view.view.ontouchlistener;
Public Class SingleMenuiteMactivity Extends Activity Implements OnClicklistener, Ontouchlistener, OnCompletionListener, OnBufferingUpdatelistener {
Private Button BTN_PLAY, BTN_PAUSE, BTN_STOP;
Private Seekbar Seekbar;
Private MediaPlayer MediaPlayer;
Private INT Lengthofaudio;
Private Final Handler Handler = New Handler ();
// XML Node Keys
Static Final String Login = "Login";
STATIC FINAL STRING PASS = "PASS";
Static Final String Key_Date = "Date";
STATIC FINAL STRING KEY_FROM = "FROM_";
Static Final String Key_To = "To_";
STATIC FINAL STRING KEY_DURATION = "DURATION";
// Static Final String Key_Typemsg = "TypeSG";
STATIC FINAL STRING KEY_FILENAME = "FILENAME";
Private Final String Path = Environment .GetExternalStoragedirectory () + "/ Android / Data / IDEV.Sedek.ekeyMangorecords / cache /";
String Filename, Data, ErrorCode;
INT DATA1;
int downloaddsize = 0;
int TotalSize = 0;
int kilobytes = 0;
Private progressdialog PD;
Private Final RunnaBle R = New Runnable () {
@Override
Public void Run () {
Updateseekprogress ();
}
};
@Override
Public Void OnCreate (Bundle SavedInstanceState) {
Super.ONCREATE (SavedInstanceState);
setContentView (R.Layout.Single_List_Item);
init ();
}
Private void init () {
BTN_PLAY = (Button) FindViewByid (R.ID.BTN_PLAY);
BTN_PLAY.SetonClicklistener (this);
BTN_PAUSE = (Button) FindViewByid (R.ID.BTN_PAUSE);
BTN_PAUSE.SetonClicklistener (this);
btn_pause.setenabled (false);
BTN_STOP = (Button) FindViewByID (R.ID.BTN_STOP);
btn_stop.setonclicklistener (this);
btn_stop.setenabled (false);
Seekbar = (Seekbar) FindViewByid (R.ID.Seekbar1);
Seekbar.setontouchlistener (this);
MediaPlayer = New MediaPlayer ();
MediaPlayer.SetonBufferingUpdatelistener (this);
MediaPlayer.SetonCompletionListener (this);
MediaPlayer.SetaudiostreamType (audioManager.stream_Music);
// Getting Intent Data
INTENT IN = GetIntent ();
// Get Xml Values From Previous Intent
String Date = IN.GetStringExtra (key_date);
String from_ = in.getStringextra (Key_From);
STRING TO_ = IN.GETSTRINGEXTRA (KEY_TO);
String Duration = IN.GetStringextra (key_duration);
// String Typemsg = in.getStringextra (Key_typemsg);
Final String FileName = IN.GetStringExtra (Key_FileName);
// Displaying All Values on the Screen
TextView LBLDATE = (TextView) FindViewByid (R.ID.Date_Label);
TextView LBLFrom_ = (textView) FindViewByID (R.ID.From__Label);
TextView LBLTO_ = (textView) FindViewByid (R.ID.TO__Label);
TextView LBLDURATION = (TextView) FindViewByID (R.ID.DURATION_LABEL);
// TextView LBLTYPEMSG = (textView) FindViewByid (R.ID.Typemsg_Label);
TextView LBLFILENAME = (textView) FindViewByid (R.ID.FileName_Label);
lbldate.settext (date);
lblfrom_.settext (from_);
lblto_.settext (to_);
LBLDURATION.SETTEXT (DURATION);
//lbltypemsg.settext (typemsg);
lblfilename.settext (FileName);
Asynctask & lt; void, void, void & gt; ast = new asynctask & lt; void, void, void & gt; () {
@Override
PROTECTED VOID ONPREEXECUTE () {
// TODO AUTO-GENERATED METHOD STUB
pd = new progressdialog (singlemenuitemactivity.this);
PD.Settitle ("File Loading");
PD.SetMessage ("wait ...");
PD.SetProgressStyle (progressdialog.style_horizontal);
pd.show ();
}
@Override
PROTECTED VOID DOINBACKGROUND (Void ... Params) {
// TODO AUTO-GENERATED METHOD STUB
getfile ();
RETURN NULL;
}
@Override
PROTECTED VOID ONPOSTEXECUTE (Void Result) {
// TODO AUTO-GENERATED METHOD STUB
pd.dismiss ();
Try {
INTENT IN = GetIntent ();
String FileName = in.getStringExtra (Key_FileName);
MediaPlayer.SetDataSource (Path + FileName);
mediaplayer.prepare ();
Lengthofaudio = mediaplayer.getduration ();
} Catch (Exception E) {
}
}
};
ast.execute ();
}
Public Void GetFile () {
INTENT IN = GetIntent ();
String FileName = in.getStringExtra (Key_FileName);
String login1 = in.getStringextra (Login);
String Pass1 = In.GetStringextra (PASS);
Try {
String audio_path = "http://92.75.25.212:8080/imap/attachments/"+FileName;
Audio_path = audio_path.replaceall ("", "% 20");
String Username = login1;
String Password = pass1;
String UserPassword = UserName + ":" + Password;
URL URL = NEW URL (Audio_Path);
HttpurlConnection C = (httpurlconnection) url.openconnection ();
C.SetRequestmethod ("Get");
C.Setdooutput (TRUE);
String Encoded = New String (Base64.Encode (UserPassword.getBytes (), Base64.no_Close));
C.SetRequestProperty ("Authorization", "Basic" + Encoded);
C.Connect ();
File File = New File (Path);
file.mkdirs ();
String FileName = FileName;
File OutputFile = New File (File, FileName);
FileOutputStream FOS = New FileoutPutStream (OutputFile);
Inputstream is = c.getInputStream ();
TOTALSIZE = C.GetContentLength ();
kilobytes = totalsize / 1024;
Runonuithread (New Runnable () {
Public void Run () {
PD.Setmax (kilobytes);
TextView FileSizeView = (textView) FindViewByid (R.ID.FileSizeView);
filesizeview.settext (string.valueof (kilobytes));
}
});
BYTE [] Buffer = New Byte [1024];
int bufferlength = 0;
while ((bufferlength = is.read (buffer)! = -1) {
FOS.WRITE (Buffer, 0, BufferLength);
downloadedsize + = bufferlength;
Final int downloaddsize1 = downloadedsize / 1024; // kilobytes.
// Update The ProgressBar //
Runonuithread (New Runnable () {
Public void Run () {
pd.setprogress (downloadedsize1);
Float Per = ((Float) downloadedsize1 / kilobytes) * 100;
PD.SetMessage ("Download" + downloadedSize1 + "Kb from" + kilobytes + "kb (" + (int) per + "%)");
}
});
}
FOS.Close ();
is.close ();
} Catch (MalForMedurlexception E) {
E.PrintStackTrace ();
} Catch (IoException E) {
E.PrintStackTrace ();
}
}
@Override
Public Void OnBufferingUpdate (Mediaplayer MediaPlayer, Int Percent) {
Seekbar.setSecondaryProgress (Percent);
}
@Override
Public Void OnCompletion (MediaPlayer MP) {
BTN_PLAY.setenabled (TRUE);
btn_pause.setenabled (false);
btn_stop.setenabled (false);
}
@Override
Public Boolean OnTouch (View V, MotionEvent Event) {
if (MediaPlayer.Isplaying ()) {
Seekbar TMPSEEKBAR = (Seekbar) V;
MediaPlayer.Seekto ((Lengthofaudio / 100) * TMPSEEKBAR.getProgress ());
}
RETURN FALSE;
}
@Override
Public Void OnClick (View View) {
Switch (view.getid ()) {
Case R.ID.BTN_Play:
Playaudio ();
Break;
Case R.ID.BTN_PAUSE:
pauseaudio ();
Break;
Case R.ID.BTN_STOP:
stopaudio ();
Break;
Default:
Break;
}
Updateseekprogress ();
}
Private Void UpdateSeekProgress () {
if (MediaPlayer.Isplaying ()) {
seekbar.setprogress ((int) (((Float) mediaplayer.getcurrentposition () / lengthofaudio) * 100));
Handler.postdelayed (R, 1000);
}
}
Private void Stopaudio () {
// mediaplayer.stop ();
Try {
INTENT IN = GetIntent ();
String FileName = in.getStringExtra (Key_FileName);
mediaplayer.stop ();
mediaplayer.reset ();
MediaPlayer.SetDataSource (Path + FileName);
mediaplayer.prepareAsync ();
} Catch (Exception E) {
E.PrintStackTrace ();
}
BTN_PLAY.setenabled (TRUE);
btn_pause.setenabled (false);
btn_stop.setenabled (false);
seekbar.setprogress (0);
}
Private Void Pauseaudio () {
mediaplayer.pause ();
BTN_PLAY.setenabled (TRUE);
btn_pause.setenabled (false);
}
Private void Playaudio () {
mediaplayer.start ();
BTN_PLAY.setenabled (false);
btn_pause.setenabled (true);
btn_stop.setenabled (true);
}
@Override
Public void onbackpressed () {
mediaplayer.stop ();
mediaplayer.reset ();
mediaplayer.release ();
super.onbackpressed (); // gives an error
}
}
Stektrays Exceptions:
11-11 12: 25: 11.209: w / dalvikvm (13875): threadid = 1: thread exiting with undcaught exception ( group = 0x41a52700)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): Fatal Exception: Main
11-11 12: 25: 11.209: E / AndroidRuntime (13875): java.lang.illegalstateException
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at Android.media.mediaPlayer.Isplaying (Native Method)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at idev.sedek.ekeymanngorecords.singlemenuitemactivity.updateseekprogress (singlemenuitemactivity.java:247)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at idev.sedek.ekeymanngorecords.seklemenuitemactivity.access $ 0 (singlemenuitemactivity.java:24)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at IDEV.Sedek.ekeyMangorecords.SingleMenuITEMActivity $ 1.run (SingleMenuItemActivity.java:7)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at Android.os.Handler.HandleCallback (Handler.java:730)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at Android.os.Handler.DispatchMessage (Handler.java:92)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at Android.os.looper.loop (Looper.java:137)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at Android.app.ActivityThread.Main (ActivityThread.java:103)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at java.lang.reflect.method.invokenative (Native Method)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at java.lang.reflect.method.invoke (Method.java:25)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at com.android.Internal.os.zygoteinit $ Methodargscaller.run (zygoteinit.java:737)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at com.android.internal.os.zygoteinit.main (zygoteinit.java:53)
11-11 12: 25: 11.209: E / AndroidRuntime (13875): at dalvik.system.nativeTart.main (Native Method)
Answer 1, Authority 100%
Why did you decide that the exception occurs in the row you marked? Judging by Stacktreys, it occurs on a 247 line in the updateseekprogress ()
method, which is most likely a string with the condition if (mediaplayer.isplaying ()) ...
.
and the reason for this simple – updateseekprogress ()
continues to be called after mediaplayer.release ();
in onbackpressed ()
.
And how does Documentation on MediaPlayer.Isplaying ( )
:
Throws IllegalstateException If The Internal Player Engine Has Not Been Initialized or Has Been Released .