Android: Activity onResume -
i have 2 activities, homework , closer. have song playing in homework when button pressed, media player pauses , closer opened. in onresume function homework, i'm trying start media player again no sound plays. can me out this?
package com.cis.lab4; import java.io.ioexception; import android.media.mediaplayer; import android.os.bundle; import android.app.activity; import android.content.intent; import android.content.res.assetfiledescriptor; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class homework extends activity { final mediaplayer mediaplayer = new mediaplayer(); int media_length; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_homework); setcontentview(r.layout.activity_homework); assetfiledescriptor afd; try { afd = getassets().openfd("rev.mp3"); mediaplayer.setdatasource(afd.getfiledescriptor(),afd.getstartoffset(),afd.getlength()); mediaplayer.prepare(); } catch (ioexception e) { e.printstacktrace(); } mediaplayer.start(); button next = (button) findviewbyid(r.id.homeworkcontinue); final intent opencloser = new intent(this, endactivity.class); next.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { mediaplayer.pause(); media_length = mediaplayer.getcurrentposition(); startactivity(opencloser); } }); } public void onresume(bundle savedinstancestate){ super.onresume(); mediaplayer.seekto(media_length); mediaplayer.start(); } }
try saving media state in onpause()
method of activity instead of instead of in onclick()
event. guarantees every time onresume()
called onpause()
called before (except first time activity runs, in case onresume()
called after onstart()
.
Comments
Post a Comment