java - Android How to play media files(.mp4) from assets folder -


i have problem code wherein cant trace problem on second try player.setdatasource(afd.getfiledescriptor(), afd.getstartoffset(), afd.getlength()); cannot determine assetfiledescriptor on 1st try catch statement..

it says local variable not initialized.. if initialized null..

the app unfortunately closed..

package com.thesis.americansignlanguage;  import java.io.ioexception;  import android.app.activity; import android.content.res.assetfiledescriptor; import android.media.mediaplayer; import android.os.bundle; import android.view.surfaceholder; import android.view.surfaceview; import android.widget.textview; import android.widget.videoview;  public class videocompareclass extends activity { string get; videoview gesture; int charindex; char[] getarray;   @override protected void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);     setcontentview(r.layout.gesturecompare);     surfaceview videoview = (surfaceview)findviewbyid(r.id.sf_view);     surfaceholder holder = videoview.getholder();     bundle gotword = getintent().getextras();     = gotword.getstring("key");     textview word = (textview)findviewbyid(r.id.textview1);     word.settext(get);      assetfiledescriptor afd;     try {         afd = getassets().openfd("hello.mp4");     } catch(ioexception e1) {         e1.printstacktrace();     }      mediaplayer player = new mediaplayer();      if(player.isplaying()) {         player.reset();     }      player.setdisplay(holder);      try {         player.setdatasource(afd.getfiledescriptor(), afd.getstartoffset(), afd.getlength());         player.prepare();     }     catch(illegalargumentexception e) {         e.printstacktrace();     }     catch(illegalstateexception e) {         e.printstacktrace();     }     catch(ioexception e) {         e.printstacktrace();     }     player.start();    } } 

assetfiledescriptor afd; try {     afd = getassets().openfd("hello.mp4"); } catch(ioexception e1) {     e1.printstacktrace(); } //... try {     player.setdatasource(afd.getfiledescriptor(), afd.getstartoffset(), afd.getlength());     player.prepare(); } 

if there exception, afd variable may not have been initialized when comes second try/catch.

you can combine both try/catch blocks one:

try {     assetfiledescriptor afd = getassets().openfd("hello.mp4");     mediaplayer player = new mediaplayer();     if(player.isplaying()) {         player.reset();     }     player.setdisplay(holder);     player.setdatasource(afd.getfiledescriptor(), afd.getstartoffset(), afd.getlength());     player.prepare();     player.start(); } catch(illegalargumentexception e) {     e.printstacktrace(); } catch(illegalstateexception e) {     e.printstacktrace(); } catch(ioexception e) {     e.printstacktrace(); } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -