Wednesday, 15 January 2014

android - Mediaplayer is stopped by AsyncTask - MediaPlayer finalized without being released -



android - Mediaplayer is stopped by AsyncTask - MediaPlayer finalized without being released -

i start app downloading new files, if there there starting playback loop. fire task new media every x seconds. task called, video stops playing error mediaplayer finalized without beingness released.

its supposed download new media if there any, , update playlist.

from logging can see first play called, after error comes , then. justs calles task 1 time again , again. its stuck in there?

im sure dumb issue im missing here. im not sure why mediaplayer stopping?

mainactivity

public class mainactivity extends activity implements oncompletionlistener { int playlistindex; int currentmedia; string medianame; arraylist<integer> playlist = new arraylist<integer>(); file[] filelist; private videoview videoview; private imageview imageview; uri mediapath; private handler mhandler = new handler(); private surfaceholder holder; // shit login! public string channelid = "5e6299eb-asd6600d58fc9"; public string username = "tvasd"; public string linkid = "70casd6600d3f7d7"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on); // file mediadir = getdir("tvr", context.mode_private); // filelist = mediadir.listfiles(); seek { downloadmedia(this); updatemediatask(this); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (jsonexception e) { // todo auto-generated grab block e.printstacktrace(); } // delete files in folder // if (mediadir.isdirectory()) { // string[] children = mediadir.list(); // (int = 0; < children.length; i++) { // new file(mediadir, children[i]).delete(); // } // } } @override public void oncompletion(mediaplayer mp) { log.i("media player", "play next please!"); if (mp != null) { mp.release(); } // play next video currentmedia++; if (currentmedia > playlist.size() - 1) { currentmedia = 0; } log.d("media_counter", string.format("%d", currentmedia)); seek { playmedia(currentmedia); } grab (ioexception e) { e.printstacktrace(); } } private void playmedia(int playlistindex) throws ioexception { log.i("media player", "play media!"); string path = filelist[playlistindex].getabsolutepath(); fileinputstream fileinputstream = new fileinputstream(path); final uri uri = uri.parse(path); string filename = filelist[playlistindex].getname(); log.i("filename", path); if (filename.contains("image")) { imageview = (imageview)findviewbyid(r.id.imageview); imageview.setvisibility(view.visible); imageview.setimageuri(uri); mhandler.postdelayed(new runnable() { public void run() { imageview.setvisibility(view.gone); imageview.setimageuri(uri); oncompletion(null); } }, 4000); } else if (filename.contains("video")) { log.d("play",string.format("%s", fileinputstream.getfd())); log.i("media player", "play video!"); surfaceview surface = (surfaceview) findviewbyid(r.id.surface); holder = surface.getholder(); holder.addcallback(new surfaceholder.callback() { @override public void surfacecreated(surfaceholder holder) { log.i("surface", "created"); } @override public void surfacedestroyed(surfaceholder holder) {} @override public void surfacechanged(surfaceholder holder, int format, int width, int height) {} }); mediaplayer pl = new mediaplayer(); pl.setoncompletionlistener(this); pl.setdisplay(holder); pl.setdatasource(fileinputstream.getfd()); pl.prepare(); pl.start(); } } void createplaylist(context context) { contextwrapper cw = new contextwrapper(context); file mediadir = cw.getdir("tvr", context.mode_private); filelist = mediadir.listfiles(); if (filelist != null) { ( int = 0;i<filelist.length;i++) { log.i("fokker", filelist[i].getname()); log.i("fokker", filelist[i].getabsolutepath()); } } currentmedia = 0; mainactivity.this.runonuithread(new runnable() { public void run() { seek { playmedia(currentmedia); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } } }); } private void updatemediatask(final mainactivity a) throws ioexception, jsonexception { int delay = 1000; // delay 1 sec. int period = 3000; // repeat every 10 minutes. 60000 timer timer = new timer(); timer.scheduleatfixedrate(new timertask() { public void run() { new updatefiles(a).execute(getbasecontext(),"update"); } }, delay, period); } private void downloadmedia(context context) throws ioexception, jsonexception { new updatefiles(this).execute(context,"new"); } }

this updatefiles class:

public class updatefiles extends asynctask<object, integer, long> { public mainactivity activity; public updatefiles(mainactivity a) { activity = a;} @override protected long doinbackground(object... params) { context context = (context) params[0]; string method = (string) params[1]; file mediadir = context.getdir("tvr", context.mode_private); jsonarray channels = json.getjson("http://192.168.2.136:8080/rest/channel/"+ linkid +"/"+ username, "get"); seek { (int i=0; < channels.length(); i++) { jsonobject channel_data = channels.getjsonobject(i); string channelid = channel_data.getstring("channelid").tolowercase(); jsonarray json = json.getjson("http://192.168.2.136:8080/rest/program/"+ linkid +"/"+ username +"/" + channelid, "get"); seek { (int j=0; j < json.length(); j++) { jsonobject json_data = json.getjsonobject(j); string name = json_data.getstring("name").tolowercase(); name = name.replace("-", "_"); if (name.contains("mp4") || name.contains("png") || name.contains("jpg") || name.contains("jpeg")) { if (name.contains("mp4")) { name = "/video_"+name; } else if (name.contains("png") || name.contains("jpg") || name.contains("jpeg")) { name = "/image_"+name; } string _name = name.replace("/", ""); file file = new file(mediadir, _name); if(file.exists()) { log.i("file found", _name); } else { new download().execute(context, name, "http://192.168.2.136:8080/rest/transfer/"+ linkid +"/"+ username +"/" + json_data.getstring("id")); } } } } grab (jsonexception e) { // todo auto-generated grab block e.printstacktrace(); } } } grab (jsonexception e) { e.printstacktrace(); } grab (nullpointerexception e) { e.printstacktrace(); } log.i("updatefiles done", method); if ( method == "new" ) { log.i("xxx", "this new method, play now"); activity.createplaylist(context); } else { log.i("xxx", "this update method, update, dont play again. go on playing"); // activity.filelist = mediadir.listfiles(); } homecoming null; } }

the log:

02-21 16:16:13.429: i/file found(24010): video_tvr_webtrailer2.mp4 02-21 16:16:13.429: i/file found(24010): video_tvr_consumerintro.mp4 02-21 16:16:13.429: i/json(24010): http://192.168.2.136:8080/rest/program/70c8223f-e054-4f33asd3f7d7/asdco/6cfbasdbe-a16600d5ac24 02-21 16:16:13.476: d/json result(24010): [{"channelid":"6casdd2-47f3-95be-a16600d5ac24","channelpath":"\\asda","datechanged":"2\/15\/2013 10:59:20 am","datecreated":"2\/15\/2013 12:58:59 pm","id":"5e25edasd-b82b-a16600d5f4ef","lastwriteutc":"\/date(1352527205000+0200)\/","name":"video_05-safety-ccw_mynhardt.wmv","size":125727228}] 02-21 16:16:13.476: i/json(24010): **http://192.asd0/rest/program/7asd4-4f33-9b6b-a1660asd89-be1a-a16600e0bcee** 02-21 16:16:13.523: **d/json result(24010): []** 02-21 16:16:13.523: **i/updatefiles done(24010): update** 02-21 16:16:13.523: i/xxx(24010): **this update method, update, dont play again. go on playing**

this question has been asked quite while ago. solution issue not async task creating mediaplayer field in class rather making local variable method.

currently in case part of private void playmedia() method. due beingness in method, eligible garbage collection method finishes. : -

public class mainactivity extends activity implements oncompletionlistener { private mediaplayer pl; // makes sure mediaplayer there if activity live

android android-asynctask android-mediaplayer

No comments:

Post a Comment