溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

安卓飛機(jī)大戰(zhàn)(八) 添加視頻文件

發(fā)布時(shí)間:2020-07-21 01:52:31 來源:網(wǎng)絡(luò) 閱讀:697 作者:JustMetU 欄目:移動(dòng)開發(fā)

在安卓應(yīng)用里添加一個(gè)視頻文件,需要在res中添加一個(gè)名為raw(不能亂起)的文件夾,里面存放你要播放的視頻文件


代碼如下:

1.在lanyout中添加一個(gè)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獲得焦點(diǎn)
                try{
                    video.start();//開始播放視頻
                }catch(Exception e){
                    e.printStackTrace();
                }
                video.setOnCompletionListener(new OnCompletionListener() {
                    
                    
                    public void onCompletion(MediaPlayer arg0) {
                        
                        finish();//結(jié)束
                    }
               });

}



這樣就可以播放視頻啦!??!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI