安卓飛機大戰(八) 添加視頻檔案

來源:互聯網
上載者:User

標籤:播放視頻   安卓開發   uri   videoview   

在安卓應用裡添加一個視頻檔案,需要在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();//結束
                    }
               });

}



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

本文出自 “軟體學習總結” 部落格,請務必保留此出處http://bigcrab.blog.51cto.com/10626858/1685045

安卓飛機大戰(八) 添加視頻檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.