如何在启动Android应用程序时添加视频文件

时间:2013-05-13 05:49:18

标签: android android-video-player

我想在启动Android应用程序时播放视频文件。我已经尝试过,但它工作不正常。我收到了一条错误消息,它告诉我视频无法播放。我已将我的视频(.mp4扩展名)保存在原始文件夹中。我已经设置了以下路径:

path = "android.resource://com.easy.video/" + R.raw.myvideo;

这是我的java文件。

package com.easy.video;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
import android.widget.VideoView;

@SuppressWarnings("unused")
public class MainActivity extends Activity {

    private VideoView videoView;
    String extStorageDirectory;
    protected static final int PLAY = 0x101;
    protected static final int STOP = 0x102;
    protected static final int PAUSE = 0x103;
    int State;
    private String current;

    private String path;
    private VideoView mVideoView;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);
        path = "android.resource://com.easy.video/" + R.raw.myvideo;
        mVideoView = (VideoView) findViewById(R.id.video);
        if (path == null || path.length() == 0) {
            Toast.makeText(MainActivity.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();

        } else {
            // If the path has not changed, just start the media player
            if (path.equals(current) && mVideoView != null) {
                mVideoView.start();
                mVideoView.requestFocus();
                return;
            }
            current = path;
            mVideoView.setVideoPath(path);
            mVideoView.start();
            mVideoView.requestFocus();
        }
        mVideoView.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer arg0) {

                Intent in = new Intent(MainActivity.this, NextActivity.class);
                startActivity(in);
                finish();
            }
        });
    }

}

其实我不确定它是否正确。 如果有人能够如此友好地解释如何做到这一点,我将不胜感激?

1 个答案:

答案 0 :(得分:0)

使用Raw文件资源ID时,请使用Uri.parse()和mVideoView.setVideoURI(Uri.parse(path));