030.權限Permission

把影片檔分別放在sdcard、res/raw/、網路上的撥放情況

  • 布局只有一個VideoView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

  • 把檔案放到sdcard,此例是放一個影片檔

package com.example.app30;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

    VideoView videoView;
    MediaController mc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        videoView = (VideoView) this.findViewById(R.id.videoView1);
        videoView.setMediaController(new MediaController(this));
        videoView.setMediaController(mc = new MediaController(this));

        videoView.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                // 把控制按鈕放到影片上,如果沒有設定控制按鈕會在畫面最下方。
                mc.setAnchorView(videoView);
                //開始撥放
                videoView.start();
            }
        });

        // 影片檔案放在sdcard上面,記得要設定權限「WRITE_EXTERNAL_STORAGE」。
        // videoView.setVideoPath("/sdcard/wash_car.mp4");

        // 影片檔案放在資源raw目錄,可以不用設定權限。
        videoView.setVideoURI(Uri.parse("android.resource://com.example.app30/" + R.raw.wash_car));

        // 影片檔案在網路上,要加權限設定「INTERNET」
        // videoView.setVideoURI(Uri.parse("rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov"));

        // 手機與電腦連線,在手機上撥放電腦正在撥放的畫面。(要設定IP、連接埠、位置目錄)
        // videoView.setVideoURI(Uri.parse("rtsp://192.168.137.1:8554/xxx"));

    }
}
  • 無 MediaController.setAnchorView的情況

  • 權限設定
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app30"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>