1

我想播放原始文件夹中的视频文件作为启动。但它不起作用。任何人都可以帮我解决这个问题。

这是我的代码

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>
4

1 回答 1

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);
        }
    }
}
于 2013-07-30T10:26:48.053 回答