2015-10-08

【Android Studio】播放影片/視頻 -- I VideoView 篇

參考資料 ----
Streaming Video in Android Apps
Media Playback
Sample Code: Media Sample for Android* Basic Media Player
Android 支援的多媒體種類


Androidmanifest.xml 內容
 
...
...

<!-- 存取網路權限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 裝置不進入睡眠權限 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

...
...

<application
    ...
    ... >

    <activity
        android:name=".MymediaplayerMainActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

...
...

</application>
 

主程式畫面內容:
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MymediaplayerMainActivity">

    <VideoView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myVideo"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>
 

Java 程式內容:
 
...
...

public class MymediaplayerMainActivity extends ActionBarActivity
{
    private VideoView vidView;
    private MediaController vidControl;
    String vidAddress = "rtsp://192.168.0.199/myvideo.mp4";

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

        vidView = (VideoView) findViewById(R.id.myVideo);
        vidControl = new MediaController(this);
        vidControl.setAnchorView(vidView);
        vidView.setMediaController(vidControl);

        Uri vidUri = Uri.parse(vidAddress);
        vidView.setVideoURI(vidUri);
        vidView.start();
    }

}