Android - video view with progressive streaming -


streamingmediaplayer .java mediaplayer not yet support streaming external urls class provides pseudo-streaming function downloading content incrementally & playing enough audio in our temporary storage.

but code reporting error...and not playing video showing stream progress

    public class streamingmediaplayer {                          private static final int intial_kb_buffer =  96*10/8;//assume 96kbps*10secs/8bits per byte                      private textview textstreamed;                      private imagebutton playbutton;                      private progressbar progressbar;                      //  track display progressbar                     private long medialengthinkb, medialengthinseconds;                     private int totalkbread = 0;                      // create handler call view updates on main ui thread.                     private final handler handler = new handler();                      private mediaplayer mediaplayer;//mediaplayer                      private file downloadingmediafile; //old location                      private boolean isinterrupted;                      private context context;                      private int counter = 0;                      public streamingmediaplayer(context  context,textview textstreamed, imagebutton playbutton, button  streambutton,progressbar    progressbar)                      {                         this.context = context;                         this.textstreamed = textstreamed;                         this.playbutton = playbutton;                         this.progressbar = progressbar;                     }                      /**                        * progressively download media temporary location , update mediaplayer new content becomes available.                      */                       public void startstreaming(final string mediaurl, long  medialengthinkb, long   medialengthinseconds) throws ioexception {                          this.medialengthinkb = medialengthinkb;                         this.medialengthinseconds = medialengthinseconds;                          runnable r = new runnable() {                                public void run() {                                    try {                                        downloadaudioincrement(mediaurl);                                 } catch (ioexception e) {                                     log.e(getclass().getname(), "unable initialize mediaplayer fileurl=" + mediaurl, e);                                     return;                                 }                                }                            };                            new thread(r).start();                     }                      /**                        * download url stream temporary location , call setdatasource                        * local file                      */                       public void downloadaudioincrement(string mediaurl) throws ioexception {                          urlconnection cn = new url(mediaurl).openconnection();                            cn.connect();                            inputstream stream = cn.getinputstream();                         if (stream == null) {                             log.e(getclass().getname(), "unable create inputstream mediaurl:" + mediaurl);                         }                          downloadingmediafile = new file(context.getcachedir(),"downloadingmedia_" + (counter++) + ".dat");                         fileoutputstream out = new fileoutputstream(downloadingmediafile);                            byte buf[] = new byte[16384];                         int totalbytesread = 0, incrementalbytesread = 0;                         {                             int numread = stream.read(buf);                                if (numread <= 0)                                    break;                                out.write(buf, 0, numread);                             totalbytesread += numread;                             incrementalbytesread += numread;                             totalkbread = totalbytesread/1000;                              testmediabuffer();                             firedataloadupdate();                         } while (validatenotinterrupted());                             stream.close();                         if (validatenotinterrupted()) {                             firedatafullyloaded();                         }                     }                        private boolean validatenotinterrupted() {                         if (isinterrupted) {                             if (mediaplayer != null) {                                 mediaplayer.pause();                                 //mediaplayer.release();                             }                             return false;                         } else {                             return true;                         }                     }                       /**                      * test whether need transfer buffered data mediaplayer.                      * interacting mediaplayer on non-main ui thread can causes crashes perform using handler.                      */                       private void  testmediabuffer() {                         runnable updater = new runnable() {                             public void run() {                                 if (mediaplayer == null) {                                     //  create mediaplayer once have minimum buffered data                                     if ( totalkbread >= intial_kb_buffer) {                                         try {                                             startmediaplayer();                                         } catch (exception e) {                                             log.e(getclass().getname(), "error copying buffered conent.", e);                                                        }                                     }                                 } else if ( mediaplayer.getduration() - mediaplayer.getcurrentposition() <= 1000 ){                                      //  note:  media player has stopped @ end transfer existing buffered data                                     //  test < 1second of data because media player can stop when there still                                     //  few milliseconds of data left play                                     transferbuffertomediaplayer();                                 }                             }                         };                         handler.post(updater);                     }                      private void startmediaplayer() {                         try {                                file bufferedfile = new file(context.getcachedir(),"playingmedia" + (counter++) + ".dat");                             movefile(downloadingmediafile,bufferedfile);                              log.e("player",bufferedfile.length()+"");                             log.e("player",bufferedfile.getabsolutepath());                              mediaplayer = new mediaplayer();                             mediaplayer.setdatasource(bufferedfile.getabsolutepath());                             mediaplayer.setaudiostreamtype(audiomanager.stream_music);                             mediaplayer.prepareasync();                             firedatapreloadcomplete();                          } catch (ioexception e) {                             log.e(getclass().getname(), "error initializing mediaplaer.", e);                             return;                         }                        }                      /**                      * transfer buffered data mediaplayer.                      * interacting mediaplayer on non-main ui thread can causes crashes perform using handler.                      */                       private void transferbuffertomediaplayer() {                         try {                             // first determine if need restart player after transferring data...e.g. perhaps user pressed pause                             boolean wasplaying = mediaplayer.isplaying();                             int curposition = mediaplayer.getcurrentposition();                             mediaplayer.pause();                              file bufferedfile = new file(context.getcachedir(),"playingmedia" + (counter++) + ".dat");                             //fileutils.copyfile(downloadingmediafile,bufferedfile);                              mediaplayer = new mediaplayer();                             mediaplayer.setdatasource(bufferedfile.getabsolutepath());                             //mediaplayer.setaudiostreamtype(audiosystem.stream_music);                             mediaplayer.prepareasync();                             mediaplayer.seekto(curposition);                              //  restart if @ end of prior beuffered content or mediaplayer playing.                               //  note:  test < 1second of data because media player can stop when there still                             //  few milliseconds of data left play                             boolean atendoffile = mediaplayer.getduration() - mediaplayer.getcurrentposition() <= 1000;                             if (wasplaying || atendoffile){                                 mediaplayer.start();                             }                         }catch (exception e) {                             log.e(getclass().getname(), "error updating newly loaded content.", e);                                           }                     }                      private void firedataloadupdate() {                         runnable updater = new runnable() {                             public void run() {                                 textstreamed.settext((charsequence) (totalkbread + " kb read"));                                 float loadprogress = ((float)totalkbread/(float)medialengthinkb);                                 progressbar.setsecondaryprogress((int)(loadprogress*100));                             }                         };                         handler.post(updater);                     }                      /**                      * have preloaded enough content , started mediaplayer update buttons & progress meters.                      */                     private void firedatapreloadcomplete() {                         runnable updater = new runnable() {                             public void run() {                                 mediaplayer.start();                                 startplayprogressupdater();                                 playbutton.setenabled(true);                                 //streambutton.setenabled(false);                             }                         };                         handler.post(updater);                     }                      private void firedatafullyloaded() {                         runnable updater = new runnable() {                              public void run() {                                 transferbuffertomediaplayer();                                 textstreamed.settext((charsequence) ("audio full loaded: " + totalkbread + " kb read"));                             }                         };                         handler.post(updater);                     }                      public mediaplayer getmediaplayer() {                         return mediaplayer;                     }                      public void startplayprogressupdater() {                         float progress = (((float)mediaplayer.getcurrentposition()/1000)/(float)medialengthinseconds);                         progressbar.setprogress((int)(progress*100));                          if (mediaplayer.isplaying()) {                             runnable notification = new runnable() {                                 public void run() {                                     startplayprogressupdater();                                 }                             };                             handler.postdelayed(notification,1000);                         }                     }                          public void interrupt() {                         playbutton.setenabled(false);                         isinterrupted = true;                         validatenotinterrupted();                     }                      public void movefile(file   oldlocation, file   newlocation)                     throws ioexception {                          if ( oldlocation.exists( )) {                             bufferedinputstream  reader = new bufferedinputstream( new fileinputstream(oldlocation) );                             bufferedoutputstream  writer = new bufferedoutputstream( new fileoutputstream(newlocation, false));                             try {                                 byte[]  buff = new byte[8192];                                 int numchars;                                 while ( (numchars = reader.read(  buff, 0, buff.length ) ) != -1) {                                     writer.write( buff, 0, numchars );                                 }                             } catch( ioexception ex ) {                                 throw new ioexception("ioexception when transferring " + oldlocation.getpath() + " " + newlocation.getpath());                             } {                                 try {                                     if ( reader != null ){                                         writer.close();                                         reader.close();                                     }                                 } catch( ioexception ex ){                                     log.e(getclass().getname(),"error closing files when transferring " + oldlocation.getpath() + " " + newlocation.getpath() );                                  }                             }                         } else {                             throw new ioexception("old location not exist when transferring " + oldlocation.getpath() + " " + newlocation.getpath() );                         }                     }                 }       //java file      /*main activity*/     /* tutorial 3*/     package com.example.tutorial;      import java.io.ioexception;      import android.app.activity;     import android.os.bundle;     import android.util.log;     import android.view.view;     import android.widget.button;     import android.widget.imagebutton;     import android.widget.mediacontroller;     import android.widget.progressbar;     import android.widget.textview;     import android.widget.videoview;      public class tutorial3 extends activity {         //videoview videoview;         private button streambutton;          private imagebutton playbutton;          private textview textstreamed;          private boolean isplaying;          private streamingmediaplayer audiostreamer;          public void oncreate(bundle icicle) {              super.oncreate(icicle);              setcontentview(r.layout.tutorial3);             initcontrols();           }           private void initcontrols() {             textstreamed = (textview) findviewbyid(r.id.text_kb_streamed);             streambutton = (button) findviewbyid(r.id.button_stream);             //videoview = (videoview) findviewbyid(r.id.videoview);              streambutton.setonclicklistener(new view.onclicklistener() {                 public void onclick(view view) {                     startstreamingaudio();             }});              playbutton = (imagebutton) findviewbyid(r.id.button_play);             playbutton.setenabled(false);             playbutton.setonclicklistener(new view.onclicklistener() {                 public void onclick(view view) {                     if (audiostreamer.getmediaplayer().isplaying()) {                         audiostreamer.getmediaplayer().pause();                         playbutton.setimageresource(r.drawable.button_play);                     } else {                         audiostreamer.getmediaplayer().start();                         audiostreamer.startplayprogressupdater();                         playbutton.setimageresource(r.drawable.button_pause);                     }                     isplaying = !isplaying;             }});         }          private void startstreamingaudio() {             try {                  final progressbar progressbar = (progressbar) findviewbyid(r.id.progress_bar);                 if ( audiostreamer != null) {                     audiostreamer.interrupt();                 }                     audiostreamer = new streamingmediaplayer(this,textstreamed, playbutton, streambutton,progressbar);                 audiostreamer.startstreaming("http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4",1677, 214);                 //streambutton.setenabled(false);             } catch (ioexception e) {                 log.e(getclass().getname(), "error starting stream audio.", e);                               }          }     }           //xml file     <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     android:padding="10px" >      <textview         android:id="@+id/text_kb_streamed"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="streaming .mp3 audio tutorial"         android:textstyle="bold" />      <button         android:id="@+id/button_stream"         style="?android:attr/buttonstylesmall"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margintop="10px"         android:text="start streaming" />      <progressbar         android:id="@+id/progress_bar"         style="?android:attr/progressbarstylehorizontal"         android:layout_width="200px"         android:layout_height="wrap_content" />      <imagebutton         android:id="@+id/button_play"         style="?android:attr/buttonstylesmall"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margintop="5px"         android:src="@drawable/button_pause" />   </linearlayout>         

the above code reporting unknown errors error(-38,0)


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -