media player - Android MediaPlayer seekTo() function working incorrect -
i have problem when using mediaplayer
play video url.
the follow of program : create activity play video -> press home button -> app.
the requirement video play start, when press home button video continuos play(only audio), , when app, video must play continuous(combine movie , audio).
the first, created mediaplayer
variable play video. when home button pressed, mediaplayer
play continuous. when app, video cannot displayed(black screen). so, created other mediaplayer
variable solved issue. when using seekto()
function seek second mediaplayer position of first mediaplayer
, function working incorrect.
import java.io.ioexception; import com.example.videotest.r; import android.app.activity; import android.media.audiomanager; import android.media.mediaplayer; import android.media.mediaplayer.onbufferingupdatelistener; import android.media.mediaplayer.oncompletionlistener; import android.media.mediaplayer.onpreparedlistener; import android.media.mediaplayer.onseekcompletelistener; import android.media.mediaplayer.onvideosizechangedlistener; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.view.ontouchlistener; import android.view.window; import android.view.view.onclicklistener; import android.view.viewgroup.layoutparams; import android.widget.button; import android.widget.framelayout; import android.widget.linearlayout; import android.widget.toast; public class playvideoactivity extends activity implements onclicklistener,surfaceholder.callback, fgmediacontroller.mediaplayercontrol { surfaceview videosurface,videosurfacefullscreen,videosurfacesmallsceen; mediaplayer player, backupplayer; fgmediacontroller controller; linearlayout llcontrollerbar; public boolean isfullscreen = false; int videoduration; int videocurrent; surfaceholder viewholder = null; boolean ispaused = false; string videourl = "http://cdn-smatopi.tv-bank.com/movie/867/867-32/867-32-2015042306-d3/hls/hls3/867-32-2015042306-d3-hls3.m3u8"; button btnseek, btnrelease; int currentpos = 30000; boolean videoprepared = false; boolean backupprepared = false; boolean mvideosizeisset = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.playvideo); btnseek = (button) findviewbyid(r.id.btnseek); btnrelease = (button) findviewbyid(r.id.btnrelease); videosurface = (surfaceview)findviewbyid(getresources().getidentifier("vvvideo", "id", getpackagename())); surfaceholder videoholder = videosurface.getholder(); videoholder.addcallback(this); player = new mediaplayer(); backupplayer = new mediaplayer(); controller = new fgmediacontroller(this); try { player.setaudiostreamtype(audiomanager.stream_music); player.setdatasource(this, uri.parse(videourl)); player.prepareasync(); //backupplayer.setaudiostreamtype(audiomanager.stream_music); //backupplayer.setdatasource(this, uri.parse(videourl)); //backupplayer.prepareasync(); } catch (exception e) { e.printstacktrace(); } btnseek.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub log.e("huhu",currentpos + ""); backupplayer.seekto(currentpos); } }); btnrelease.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub backupplayer.stop(); backupplayer.release(); } }); player.setonbufferingupdatelistener(new onbufferingupdatelistener() { public void onbufferingupdate(mediaplayer mp, int percent) { } }); player.setonpreparedlistener(new onpreparedlistener() { @override public void onprepared(mediaplayer mp) { controller.setmediaplayer(playvideoactivity.this); controller.setanchorview((framelayout) findviewbyid(playvideoactivity.this.getresources().getidentifier("videosurfacecontainer", "id", playvideoactivity.this.getpackagename()))); player.start(); } }); } @override public void ondestroy(){ //backupplayer.release(); player.release(); super.ondestroy(); } @override public void onresume(){ log.e("surface created","onresume"); super.onresume(); } // implement surfaceholder.callback @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacecreated(surfaceholder holder) { log.e("surface created","surface created! " + holder); if(viewholder != null){ viewholder = holder; backupplayer = new mediaplayer(); try { backupplayer.setaudiostreamtype(audiomanager.stream_music); backupplayer.setdatasource(this, uri.parse(videourl)); backupplayer.prepareasync(); backupplayer.setdisplay(holder); backupplayer.setonpreparedlistener(new onpreparedlistener() { @override public void onprepared(mediaplayer mp) { backupprepared = true; backupplayer.start(); } }); backupplayer.setonvideosizechangedlistener(new onvideosizechangedlistener() { @override public void onvideosizechanged(mediaplayer mp, int width, int height) { // todo auto-generated method stub if (width == 0 || height == 0){ toast.maketext(getapplicationcontext(),"width = 0 height = 0"+ mp.getduration(), toast.length_long).show(); }else{ if(backupprepared){ backupprepared = false; currentpos = player.getcurrentposition(); player.reset(); toast.maketext(getapplicationcontext(),"seek "+ backupplayer.getcurrentposition() + " | " + backupplayer.isplaying() + "|" + currentpos, toast.length_long).show(); log.e("huhu","seek "+ backupplayer.getcurrentposition() + " | " + backupplayer.isplaying() + "|" + currentpos); backupplayer.seekto(currentpos); // if set currentpos = 20000ms or 30000ms backupplayer can seek, if set currentpos = 10000ms backupplayer cannot seek ! } } } }); } catch (exception e) { e.printstacktrace(); } }else{ player.setdisplay(holder); } } @override public void surfacedestroyed(surfaceholder holder) { log.e("surface created","surfacedestroyed! " + player.getcurrentposition()); viewholder = holder; } // end mediaplayer.onpreparedlistener // implement videomediacontroller.mediaplayercontrol @override public boolean canpause() { return true; } @override public boolean canseekbackward() { return true; } @override public boolean canseekforward() { return true; } @override public int getbufferpercentage() { return 0; } @override public int getcurrentposition() { //log.printlog(log.error, "current :"+player.getcurrentposition()); int current = 0; try{ current = player.getcurrentposition(); }catch(exception e){ e.printstacktrace(); } return current; } @override public int getduration() { int duration = 0; try{ duration = player.getduration(); }catch(exception e){ e.printstacktrace(); } return duration; } @override public boolean isplaying() { boolean isplaying = false; try{ isplaying = player.isplaying(); }catch(exception e){ e.printstacktrace(); } return isplaying; } @override public void pause() { player.pause(); } @override public void seekto(int i) { player.seekto(i); } @override public void start() { player.start(); } @override public boolean isfullscreen() { return isfullscreen; } @override public void togglefullscreen() { // todo auto-generated method stub } @override public void dobackpressed() { // todo auto-generated method stub } @override public void onclick(view v) { } }
Comments
Post a Comment