从Sd卡播放视频

时间:2012-04-10 05:37:52

标签: android android-videoview

我已经构建了一个应用程序,其中包含从Internet上传输的视频,我对它们的性能印象不是很深刻。是否有人愿意分享从SD卡加载视频的代码。

由于

2 个答案:

答案 0 :(得分:3)

我希望这段代码可以帮助你

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

使用此代码本规范,亲爱的朋友们!

答案 1 :(得分:0)

创建活动并在需要播放视频时调用它。 您可以在Intent中捆绑视频路径(无论是url还是sdcard还是资源)。 然后在您的活动中,该活动应该只包含FrameLayout VideoView,并执行以下操作:

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