Playing video from Sd card

六月ゝ 毕业季﹏ 提交于 2019-12-18 07:17:08

问题


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();         
    } 


来源:https://stackoverflow.com/questions/10083609/playing-video-from-sd-card

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