android - Do you know why mediaplayer runonuithread doesn't work? -
i trying run android's mediaplayer
using runonuithread
. did not caught exception setdatasource
. after that, nothing happens mediaplayer
. should give callback surface changed , onprepared
.
it seems mediaplayer
doesn't support way. if true, there workarounds ? need kind of logic because need info network query blocked. need run onsuccess
that.
what suggestion this? much!
onresume() { getinfo(xxx); } void getinfo(url, new datalistener() { @override public void ondatasuccess(xxx) { playvideoonsuccess(xxx); } } public void playvideoonsuccess(xxx) { mybaseactivity.runonuithread(new runnable() { @override public void run() { try { mplayerlistener = new videoplayerlistener(null, content); // create new mediaplayer mvideoplayer = videoplayer.getinstance(); mvideoplayer.setvideoplayerlistener(mplayerlistener); // setdatasource mvideoplayer.consumecontent(content); } catch (exception e) { e.printstacktrace(); } } }); }
here sample code used play video hope might in way
public class videoviewdemo extends activity { private static final string tag = "videoviewdemo"; private string current; /** * todo: set path variable streaming video url or local media * file path. */ private string path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4"; private videoview mvideoview; @override public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.videoview); mvideoview = (videoview) findviewbyid(r.id.surface_view); runonuithread(new runnable() { public void run() { playvideo(); } }); } private void playvideo() { try { // final string path = path; log.v(tag, "path: " + path); if (path == null || path.length() == 0) { toast.maketext(videoviewdemo.this, "file url/path empty", toast.length_long).show(); } else { // if path has not changed, start media player if (path.equals(current) && mvideoview != null) { mvideoview.start(); mvideoview.requestfocus(); return; } current = path; mvideoview.setvideopath(getdatasource(path)); mvideoview.start(); mvideoview.setmediacontroller(new mediacontroller(this)); mvideoview.requestfocus(); } } catch (exception e) { log.e(tag, "error: " + e.getmessage(), e); if (mvideoview != null) { mvideoview.stopplayback(); } } } private string getdatasource(string path) throws ioexception { if (!urlutil.isnetworkurl(path)) { return path; } else { url url = new url(path); urlconnection cn = url.openconnection(); cn.connect(); inputstream stream = cn.getinputstream(); if (stream == null) throw new runtimeexception("stream null"); file temp = file.createtempfile("mediaplayertmp", "dat"); temp.deleteonexit(); string temppath = temp.getabsolutepath(); fileoutputstream out = new fileoutputstream(temp); byte buf[] = new byte[128]; { int numread = stream.read(buf); if (numread <= 0) break; out.write(buf, 0, numread); } while (true); try { stream.close(); } catch (ioexception ex) { log.e(tag, "error: " + ex.getmessage(), ex); } return temppath; } } }
this work me
Comments
Post a Comment