android:video is not playing -
i trying run video stored in assets/pages/id(id may rice or sugar or meat ect)/id.mp4
. in app if touch on video of item (rice or sugar or meat
) play respective mp4 video. video not playing properly.
trying find out fail.
below code
package com.app.teachmesushi; import java.io.file; import java.io.fileoutputstream; import java.io.inputstream; import java.util.hashmap; import java.util.map; import android.app.activity; import android.app.progressdialog; import android.content.componentname; import android.content.context; import android.content.intent; import android.content.serviceconnection; import android.content.res.assetmanager; import android.graphics.pixelformat; import android.media.mediaplayer; import android.media.mediaplayer.onpreparedlistener; import android.os.asynctask; import android.os.bundle; import android.os.environment; import android.os.handler; import android.os.ibinder; import android.os.message; import android.os.messenger; import android.os.remoteexception; import android.util.log; import android.widget.mediacontroller; import android.widget.videoview; import com.flurry.android.flurryagent; public class videoactivity extends activity implements onpreparedlistener { private videoview video; private mediacontroller ctlr; private string id; private file file; private progressdialog pd = null; private integer msec = -1; private int start = 1; messenger mservice = null; boolean misbound; final messenger mmessenger = new messenger(new incominghandler()); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().setformat(pixelformat.translucent); setcontentview(r.layout.activity_video); final string id = getintent().getextras().getstring("id"); this.id = id; if (savedinstancestate != null) { msec = savedinstancestate.getint("pos"); } video = (videoview) findviewbyid(r.id.video); ctlr = new mediacontroller(this, false); ctlr.setmediaplayer(video); video.setmediacontroller(ctlr); video.setonpreparedlistener(this); } @override protected void onstart() { super.onstart(); flurryagent.onstartsession(this, mainactivity.flurry_key); map<string, string> articleparams = new hashmap<string, string>(); articleparams.put("id", id); // capture user status flurryagent.logevent("video", articleparams); log.e("sushi", "msec: " + msec); log.e("sushi", "start: " + start); checkifserviceisrunning(); string filename = id + ".mp4"; file = new file(getexternalfilesdir(null), filename); if (file.exists()) { video.setvideopath(file.getpath()); } else { // show progressdialog on thread pd = progressdialog.show(videoactivity.this, "launching video", "accessing...", true, false); pd.dismiss(); // start new thread download data new downloadtask().execute(filename); } if (msec != -1) { video.seekto(msec); } else if (start == 1) { start = 0; video.start(); } else if (msec == video.getduration()) { video.seekto(0); } } @override public void onpause() { super.onpause(); video.pause(); msec = video.getcurrentposition(); pd.dismiss(); } @override public void onstop() { super.onstop(); video.pause(); msec = video.getcurrentposition(); dounbindservice(); } @override public void ondestroy() { super.ondestroy(); video = null; ctlr = null; } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("pos", video.getcurrentposition()); } public void onprepared(mediaplayer mp) { try { video.requestfocus(); ctlr.show(); } catch (exception e) { } } class downloadtask extends asynctask<string, void, object> { protected object doinbackground(string... args) { assetmanager = getassets(); string filename = args[0]; file file = new file(getexternalfilesdir(null), filename); log.i("sushi", "background thread starting"); string state = environment.getexternalstoragestate(); if (environment.media_mounted.equals(state)) { try { inputstream in = am.open("pages/" + id + "/" + id + ".mp4"); //inputstream in = am.open("http://176.9.35.93/tmc/videos/old/equipments.mp4"); fileoutputstream f = new fileoutputstream(file); byte[] buffer = new byte[1024]; int len1 = 0; while ((len1 = in.read(buffer)) > 0) { f.write(buffer, 0, len1); } f.close(); in.close(); } catch (exception e) { log.d("sushi", e.getmessage()); } if (videoactivity.this.pd != null) { videoactivity.this.pd.dismiss(); videoactivity.this.pd = null; } } return null; } protected void onpostexecute(object result) { intent intent = new intent(videoactivity.this, videoactivity.class); intent.addflags(intent.flag_activity_clear_top); bundle b = new bundle(); b.putstring("id", id); intent.putextras(b); startactivity(intent); finish(); } } @override public void onbackpressed() { try { log.d("sushi", "deleting file"); file.delete(); } catch (exception e) { // todo auto-generated catch block log.e("sushi", "file delete failed"); } finish(); } private serviceconnection mconnection = new serviceconnection() { public void onserviceconnected(componentname classname, ibinder service) { mservice = new messenger(service); try { message msg = message.obtain(null, timerservice.msg_register_client); msg.replyto = mmessenger; mservice.send(msg); } catch (remoteexception e) { // in case service has crashed before // } } public void onservicedisconnected(componentname classname) { // called when connection service has been // unexpectedly disconnected - process crashed. mservice = null; } }; private void checkifserviceisrunning() { // if service running when activity starts, want // automatically bind it. if (timerservice.isrunning()) { dobindservice(); } else { log.e("sushi", "service not running"); } } void dobindservice() { bindservice(new intent(this, timerservice.class), mconnection, context.bind_auto_create); misbound = true; log.e("sushi", "bound service"); } void dounbindservice() { if (misbound) { // if have received service, , hence registered it, // time unregister. if (mservice != null) { try { message msg = message.obtain(null, timerservice.msg_unregister_client); msg.replyto = mmessenger; mservice.send(msg); } catch (remoteexception e) { // there nothing special need if service has // crashed. } } // detach our existing connection. unbindservice(mconnection); misbound = false; } } class incominghandler extends handler { @override public void handlemessage(message msg) { switch (msg.what) { case timerservice.msg_set_int_value: log.e("sushi", string.valueof(msg.arg1)); if (msg.arg1 <= 1) { video.pause(); } break; default: super.handlemessage(msg); } } } }
my logcat error is
04-25 21:15:57.371: e/sushi(273): msec: -1 04-25 21:15:57.371: e/sushi(273): start: 1 04-25 21:15:57.381: e/sushi(273): service not running 04-25 21:15:58.121: e/sushi(273): msec: -1 04-25 21:15:58.121: e/sushi(273): start: 1 04-25 21:15:58.121: e/sushi(273): service not running
pls help
everything ok
. problem run service in emulator not in real device. when test in real device running properly. problem in emulator.