问题
I am trying to play video from sub-folder's of Assets folder.
I have created one folder xyz in assets folder.
this is my code:
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();
But it is giving me error E/MediaPlayer(11068): error (1, -2147483648)
also tried this code:
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();
}
Thanks for help in advance :)
回答1:
Replace this:
String uriPath = "android.resource://"+ getPackageName() + "/assets/xyz/"+"age.mp4";
with this:
AssetFileDescriptor afd = getAssets().openFd("xyz/age.mp4"); player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
and make sure you get ID of video view from layout like:
VideoView videoView =(VideoView)findViewById(R.id.videoView1);
Create MediaController like:
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Set MediaController like:
videoView.setMediaController(mediaController);
回答2:
try this:
AssetFileDescriptor afd = getAssets().openFd("test/xyz/AudioFile.mp4");
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
来源:https://stackoverflow.com/questions/21955248/how-to-play-video-from-sub-folder-of-assets-folder-in-android