how to play video from sub folder of assets folder in android

好久不见. 提交于 2020-01-03 03:23:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!