我正在尝试从Assets文件夹的子文件夹中播放视频。
我在assets文件夹中创建了一个文件夹xyz。
这是我的代码:
String uriPath = "android.resource://"+ getPackageName() + "/assets/xyz/"+"age.mp4";
videoView.setVideoURI(Uri.parse(uri));
// videoView.setMediaController(new MediaController(this));
videoView.setMediaController(null);
videoView.requestFocus();
videoView.start();
但它给了我错误E/MediaPlayer(11068): error (1, -2147483648)
也试过这段代码:
public void Play(String fileName) {
AssetFileDescriptor descriptor = null;
try {
String[] array = getApplicationContext().getAssets().list("long_a");
Toast.makeText(MainActivity.this,
"file is exists " + array.length, Toast.LENGTH_LONG)
.show();
descriptor = getApplicationContext().getAssets().openFd(
"xyz/say.mp4");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long start = descriptor.getStartOffset();
long end = descriptor.getLength();
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), start,
end);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
}
提前感谢您的帮助:)
答案 0 :(得分:2)
替换它:
String uriPath =“android.resource://”+ getPackageName()+“/ assets / xyz /”+“age.mp4”;
用这个:
AssetFileDescriptor afd = getAssets()。openFd(“xyz / age.mp4”); player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
并确保您从布局中获取视频视图的ID,如:
VideoView videoView =(VideoView)findViewById(R.id.videoView1);
创建MediaController,如:
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
将MediaController设置为:
videoView.setMediaController(的MediaController);
答案 1 :(得分:1)
试试这个:
AssetFileDescriptor afd = getAssets().openFd("test/xyz/AudioFile.mp4");
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());