温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

安卓飞机大战(八) 添加视频文件

发布时间:2020-07-21 01:52:31 来源:网络 阅读:685 作者:JustMetU 栏目:移动开发

在安卓应用里添加一个视频文件,需要在res中添加一个名为raw(不能乱起)的文件夹,里面存放你要播放的视频文件


代码如下:

1.在lanyout中添加一个videoview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

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

</LinearLayout>




2.在MainActivity中:


import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

    
    private VideoView video;
    private Button button1;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.begin_video);
        
                video=(VideoView)findViewById(R.id.video1);
                button1=(Button)findViewById(R.id.button1);
                Uri uri=Uri.parse("android.resource://com.example.agame/"+R.raw.startvideo);//获得uri                video.setVideoURI(uri);//指定要播放的视频

                video.requestFocus();让video获得焦点
                try{
                    video.start();//开始播放视频
                }catch(Exception e){
                    e.printStackTrace();
                }
                video.setOnCompletionListener(new OnCompletionListener() {
                    
                    
                    public void onCompletion(MediaPlayer arg0) {
                        
                        finish();//结束
                    }
               });

}



这样就可以播放视频啦!!!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI