在videoview android中播放视频

时间:2014-06-03 01:50:38

标签: android android-videoview

我正在尝试在视频中播放视频。 xml代码包含视频视图:

<VideoView
    android:id="@+id/vvIntroVideo"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:layout_weight="38" />

在代码的Java部分我有:

public class LoginPage extends Activity {

private VideoView introVideoView;

private static String DEBUG = LoginPage.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_page);

    //initializing part
    introVideoView = (VideoView) findViewById(R.id.vvIntroVideo);

    try {
        introPlayer();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.v(DEBUG, e.getMessage().toString());
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login_page, menu);
    return true;
}

public void introPlayer(){
    Uri video = Uri.parse("android:resource://"+getPackageName()+"/"+R.raw.documentariesandyou);
    introVideoView.setVideoURI(video);
    introVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.setLooping(true);
        }
    });
    introVideoView.start();
}
}

应用程序显示无法播放视频的错误。该视频是在MP4中,因此可能没有问题。在这种情况下,任何人都可以帮助我。

3 个答案:

答案 0 :(得分:0)

首先定义ProgressDialog mProgressDialog;

然后尝试这段代码,它对我有用:

video = (VideoView) findViewById(R.id.VideoView1);
        final MediaController mc = new MediaController(this);
        mc.setAnchorView(video);
        mc.setMediaPlayer(video);
        Uri uri = Uri.parse(path1);
        video.setMediaController(mc);
        System.out.println("!-- uri: "+uri.toString());
        video.setVideoURI(uri);

        video.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {



                   mProgressDialog.dismiss();






            }
        });

        video.start();

        video.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {

            }

        });


    }


    public void showLoaderProgress(){
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Loading Video\nPlease wait...");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

其中path1是您的视频路径。

答案 1 :(得分:0)

由于您是初学者,我将为您提供在videoview中播放视频的代码,这将播放来自原始文件夹的视频以及来自服务器网址的视频直播。玩本地只是加载 URI 到vieoview休息所有的东西都是一样的。很容易理解。我希望它能帮到你。

活动中的全局变量

private MediaController controller;
ProgressBar progressBar = null;
private String _url;

的onCreate

 VideoView video = (VideoView) findViewById(R.id.videoView1);
    controller = new MediaController(VideoScreen.this);
    video.setMediaController(controller);

    video.setVideoPath(_url);
    video.requestFocus();
    video.start();
    progressBar.setVisibility(View.VISIBLE);
    video.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.start();
            progressBar.setVisibility(View.GONE);
            mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                        int arg2) {
                    progressBar.setVisibility(View.GONE);
                    mp.start();
                }
            });

        }
    });

答案 2 :(得分:0)

我找到了解决方案。实际上问题出在视频编码上。我使用姜饼模拟器来测试应用程序,视频是在mp4 h.264。但是看起来姜饼只能在默认情况下播放mp4或3gpp h.263。