视频如飞溅

时间:2013-07-30 07:26:49

标签: android android-layout android-video-player

我想从原始文件夹中播放视频文件作为splash.But它无法正常工作。任何人都可以帮我解决这个问题。

这是我的代码

public class SplashActivity extends Activity implements OnCompletionListener{

    private MediaController mc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        VideoView vd = (VideoView) findViewById(R.id.VideoView);
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.sea);
        //mc = new MediaController(this);
        //vd.setMediaController(mc);
        vd.requestFocus();
        vd.setVideoURI(uri);
        vd.start();
        // vd.setOnCompletionListener(this);
    }

    public void onCompletion(MediaPlayer mp) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

我的布局是:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:gravity="center">

  <VideoView android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:id="@+id/VideoView">
  </VideoView>

< /LinearLayout>

1 个答案:

答案 0 :(得分:0)

public class SplashActivity extends Activity{

    private String mSplashVideoPath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash);

        mSplashVideoPath = Environment.getExternalStorageDirectory() + PATH;

        copyVideoFile();

        final VideoView mVideoView = (VideoView) findViewById(R.id.videoView);
        mVideoView.requestFocus();
        mVideoView.setVideoPath(mSplashVideoPath);
        mVideoView.start();

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {

                startActivity(new Intent(SplashActivity.this, MainActivity.class));

                finish();
            }
        });
    }

    private void copyVideoFile() {
        final File videoPath = new File(mSplashVideoPath);

        if (!videoPath.exists()) {
            videoPath.mkdirs();
        }

        final File videoFile = new File(mSplashVideoPath);

        if (!videoFile.exists()) {
            AssetManager assetManager = getAssets();
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open(ASSETS_SPLASH_VIDEO);
                out = new FileOutputStream(videoFile);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }
}