Android How to play media files(.mp4) from assets folder
-
I have a problem with my code wherein i cant trace the problem on the second try where the player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); cannot determine the AssetFileDescriptor on the 1st try catch statement.. it says that the local variable is not initialized.. but if i initialized it to null.. the app will 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(); get = 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(); } }