我的代码有问题,我无法在第二次尝试中跟踪问题,player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
无法在第一次尝试捕获语句中确定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();
}
}
答案 0 :(得分:0)
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("hello.mp4");
} catch(IOException e1) {
e1.printStackTrace();
}
//...
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
player.prepare();
}
如果存在异常,则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();
}
答案 1 :(得分:0)
在准备播放器之前必须关闭资产文件描述符。 这就是我解决问题的方法:
player = new MediaPlayer(); AssetFileDescriptor afd =
BeeDailyConvo.this.getAssets().openFd("sounds/"+file);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
afd.close();**//just added this line**
player.prepare();
player.start();
似乎是解决方案