如何在Android中创建简单的视频播放器?

时间:2014-03-29 12:34:00

标签: java android video

我试图在Android中创建一个简单的视频播放器,但我遇到了麻烦。当我切换移动屏幕时,程序再次播放视频。视频没有继续播放。 请帮我解决这个问题。对不起,我的英语不好。谢谢:D

这是xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="bottom"
    android:gravity="center" >

      <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/play" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/pause" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/stop" /> 
</LinearLayout> 

MainActivity.java

 package com.example.baitap_3;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {
    ImageButton btnPlay, btnPause, btnStop;
    VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnPlay = (ImageButton) findViewById(R.id.imageButton1);
        btnPause = (ImageButton) findViewById(R.id.imageButton2);
        btnStop = (ImageButton) findViewById(R.id.imageButton3);
        videoView = (VideoView) findViewById(R.id.videoView1);      
        String uriPath = "android.resource://com.example.baitap_3/" + R.raw.video;
        Uri uri = Uri.parse(uriPath);

        MediaController mediacontroller = new MediaController(MainActivity.this);
        mediacontroller.setAnchorView(videoView);
        videoView.setVideoURI(uri);

        videoView.requestFocus();
        videoView.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                videoView.start();
            }
        });


        // play
          btnPlay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                videoView.start();
            }
        });
        // pause
        btnPause.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                videoView.pause();
            }
        });
        // stop
        btnStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                videoView.pause();
                videoView.seekTo(0);
            }
        }); 

    }

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

}

2 个答案:

答案 0 :(得分:0)

在您的活动中使用此功能。

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
  }

答案 1 :(得分:0)

您必须在manifest中为活动定义configChanges

<activity       android:name="MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            >
        </activity>