Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> 實現簡單的Android的播放視頻功能

實現簡單的Android的播放視頻功能

編輯:Android開發實例

實現播放視頻有兩種方式,一種是使用VideoView;一種是使用SurfaceView。

VideoView

在main.xml中加入:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <VideoView android:id="@+id/videoView" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_x="10px"
            android:layout_y="10px" />
    </AbsoluteLayout>

 

 

在類中,加入以下代碼

super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//設置成全屏模式
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//強制為橫屏
        setContentView(R.layout.main);
       videoView = (VideoView) findViewById(R.id.videoView);
//       videoView.setVideoPath("/sdcard/xyx.3gp");
       videoView.setVideoURI(Uri.parse("/sdcard/beijinghuanyingni.mp4"));
       MediaController mediaController = new MediaController(this);
       videoView.setMediaController(mediaController);
       videoView.start();
       //videoView.requestFocus();

首先我使用的是全屏,強制橫屏播放視頻。.setVideoURI是設置視頻的路徑,這裡我用的是sdcard上的視頻,如果用http的,則需寫完整路徑。最重要的一點,我看網上有用videoView.requestFocus();來加載視屏播放,這裡我沒有能播放成功,改用了videoView.start();來播放。
這種方式是使用Android自帶的按鈕,無需自定義暫停、播放等按鈕控件。

源碼見:http://henzil.googlecode.com/svn/trunk/play/

SurfaceView

這種方式是使用自定義的暫停、播放等按鈕控件。

在main.xml文件中加入:

<SurfaceView android:id="@+id/surface"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
</SurfaceView>

在Java類中,實現OnBufferingUpdateListener, OnCompletionListener, MediaPlayer.OnPreparedListener,SurfaceHolder.Callback接口

代碼見:

package com.play2;

import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class PlayDemo2 extends Activity implements OnBufferingUpdateListener,
        OnCompletionListener, MediaPlayer.OnPreparedListener,
        SurfaceHolder.Callback {
    private MediaPlayer mediaPlayer;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private int videoWidth;
    private int videoHeight;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//強制為橫屏
        this.surfaceView = (SurfaceView) this.findViewById(R.id.surface);
        this.surfaceHolder = this.surfaceView.getHolder();
        this.surfaceHolder.addCallback(this);
        this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        Log.v("cat", ">>>create ok.");
    }

    private void playVideo() throws IllegalArgumentException,
            IllegalStateException, IOException {
        this.mediaPlayer = new MediaPlayer();
        this.mediaPlayer
                .setDataSource("/sdcard/daoxiang.3gp");
        this.mediaPlayer.setDisplay(this.surfaceHolder);
        this.mediaPlayer.prepare();
        this.mediaPlayer.setOnBufferingUpdateListener(this);
        this.mediaPlayer.setOnPreparedListener(this);
        this.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        Log.i("mplayer", ">>>play video");
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        Log.i("cat", ">>>surface changed");

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            this.playVideo();
        } catch (Exception e) {
            Log.i("cat", ">>>error", e);
        }
        Log.i("cat", ">>>surface created");

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.v("mplayer", ">>>surface destroyed");

    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPrepared(MediaPlayer arg0) {
        this.videoWidth = this.mediaPlayer.getVideoWidth();
        this.videoHeight = this.mediaPlayer.getVideoHeight();

        if (this.videoHeight != 0 && this.videoWidth != 0) {
            this.surfaceHolder.setFixedSize(this.videoWidth, this.videoHeight);
            this.mediaPlayer.start();
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.mediaPlayer != null) {
            this.mediaPlayer.release(); 
            this.mediaPlayer = null;
        }
    }
}

源碼見:http://henzil.googlecode.com/svn/trunk/play2/

注:videoView.requestFocus()只是做視頻的加載,並不主動開始播放視頻,得用戶手動點擊才能開始播放。而videoView.start()是開始的時候就播放,只要網絡正常,一啟動程序就會播放網絡視頻。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved