android - The correct way of using Mediaplayer to show Streaming Videos -
i've been looking best way of playing videos. tried many ways, in someway couldn't see video , heard audio. , showed video stop playing in 5 seconds. tried understand lifecycle , everytime try make work happens.
here code i've been struggling past 6 days. don't know wrong make app crash. can give me best implementation of media player streaming videos using media player?
package playingpackage; import android.app.fragment; import android.app.progressdialog; import android.media.audiomanager; import android.media.mediaplayer; import android.os.asynctask; import android.os.bundle; import android.support.annotation.nullable; import android.view.layoutinflater; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.viewgroup; import com.example.ptivi.ptivi.r; import java.io.ioexception; /** * created peyam on 2015-07-22. */ public class playvideo extends fragment implements mediaplayer.onpreparedlistener, surfaceholder.callback { private surfaceview surfaceview; private mediaplayer mediaplayer = new mediaplayer(); private string uri; private progressdialog pdialog; private surfaceholder surfaceholder; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.playvideo, container, false); uri = getarguments().getstring("url", "http://176.28.44.168:1935/live/_definst_/livestream/playlist.m3u8"); surfaceview = (surfaceview) v.findviewbyid(r.id.surfaceview); new asynctask<void, void, void>() { @override protected void doinbackground(void... params) { play(); return null; } }.execute(); return v; } private void play() { surfaceholder = surfaceview.getholder(); surfaceholder.addcallback(this); surfaceholder.settype(surfaceholder.surface_type_push_buffers); mediaplayer.setaudiostreamtype(audiomanager.stream_music); mediaplayer.setaudiostreamtype(audiomanager.stream_notification); mediaplayer.setdisplay(surfaceholder); // create pdialog pdialog = new progressdialog(getactivity()); //setting title pdialog.settitle("loading..."); // set progressbar message pdialog.setmessage("please wait"); pdialog.setindeterminate(false); pdialog.setcancelable(false); // show progressbar pdialog.show(); try { mediaplayer.setdatasource(uri); } catch (illegalargumentexception | illegalstateexception | ioexception | securityexception e) { e.printstacktrace(); } try { mediaplayer.setonpreparedlistener(this); mediaplayer.prepareasync(); } catch (illegalstateexception e) { e.printstacktrace(); } } @override public void onprepared(mediaplayer mp) { mp.start(); } @override public void onpause() { super.onpause(); mediaplayer.release(); } @override public void ondestroy() { super.ondestroy(); mediaplayer.stop(); } @override public void surfacecreated(surfaceholder holder) { } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacedestroyed(surfaceholder holder) { } }
i used exoplayer demo in order play videos. don't use emulator doesn't encode lots of videos
Comments
Post a Comment