Playing video from Sd card

2019-01-27 07:09发布

问题:

I have build an app that has videos in it that stream from the Internet and I'm not very impressed with the performance of them. Would anyone like to share the code for loading videos from the SD card.

Thanks

回答1:

I hope this code help u

public class video extends Activity{

    VideoView video_view;
    String ex_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.eccryption);
        video_view = (VideoView) findViewById(R.id.videoView1);

        ex_name = getIntent().getExtras().getString("video_name");

        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(video_view);
        video_view.setMediaController(new MediaController(this));
        handler.sendEmptyMessage(1);

    }

    Handler handler = new Handler(){

        public void handleMessage(Message msg){

            int pos=msg.what;
            if (pos == 1){

                video_view.setVideoPath(Environment.getExternalStorageDirectory()+"/"+ex_name+".mp4");
                video_view.requestFocus();
                video_view.start();

                Log.d("Before Video Finish", "i m in before video finish");
                video_view.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        finish();
                    }
                });
            }
        }
    };

Use this Code This Code,My dear friends !



回答2:

Create an activity and call it when you need to play the video. You can bundle the video path (whether url or sdcard or a resource) in the Intent. Then in your activity which should only contain a FrameLayout with a VideoView do something like:

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        mVideoPath = extras.getString(INTENT_EXTRA_URI);
        int resId = getResId(mVideoPath, R.raw.class);
                    String uriPath = null;
        if (mVideoPath.startsWith("http://") || mVideoPath.startsWith("https://")) {
            uriPath = mVideoPath;
        } else if (mVideoPatah.startsWith("/mnt/sdcard/")) {
            uriPath = mVideoPath;
        } else {
            int resId = getResId(mVideoPath, R.raw.class);
            uriPath = "android.resource://" + getResources().getResourcePackageName(resId) + "/" + resId;
        }
        mVideoView.setVideoURI(Uri.parse(uriPath));
        MediaController mediaController = new MediaController(this);
        mVideoView.setMediaController(mediaController);
        mVideoView.requestFocus();
        mVideoView.start();         
    }