我的代码有问题,我无法在第二次尝试中跟踪问题,而player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
无法在第一次尝试catch语句上确定AssetFileDescriptor。
它说本地变量没有初始化..但是如果我将其初始化为null。
该应用程序将不幸地关闭。
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();
}
}
最佳答案
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("hello.mp4");
} catch(IOException e1) {
e1.printStackTrace();
}
//...
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
}
如果有异常,则在第二次try / catch时可能未初始化
afd
变量。您可以将两个try / catch块合并为一个:
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();
}