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 內容
  1.  
  2. ...
  3. ...
  4.  
  5. <!-- 存取網路權限 -->
  6. <uses-permission android:name="android.permission.INTERNET" />
  7. <!-- 裝置不進入睡眠權限 -->
  8. <uses-permission android:name="android.permission.WAKE_LOCK" />
  9.  
  10. ...
  11. ...
  12.  
  13. <application
  14. ...
  15. ... >
  16.  
  17. <activity
  18. android:name=".MymediaplayerMainActivity"
  19. android:theme="@style/Theme.AppCompat.NoActionBar"
  20. android:screenOrientation="landscape" >
  21. <intent-filter>
  22. <action android:name="android.intent.action.MAIN" />
  23.  
  24. <category android:name="android.intent.category.LAUNCHER" />
  25. </intent-filter>
  26. </activity>
  27.  
  28. ...
  29. ...
  30.  
  31. </application>
  32.  

主程式畫面內容:
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MymediaplayerMainActivity">
  9.  
  10. <VideoView
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:id="@+id/myVideo"
  14. android:layout_alignParentLeft="true"
  15. android:layout_alignParentRight="true"
  16. android:layout_alignParentStart="true"
  17. android:layout_alignParentEnd="true"/>
  18. </RelativeLayout>
  19.  

Java 程式內容:
  1.  
  2. ...
  3. ...
  4.  
  5. public class MymediaplayerMainActivity extends ActionBarActivity
  6. {
  7. private VideoView vidView;
  8. private MediaController vidControl;
  9. String vidAddress = "rtsp://192.168.0.199/myvideo.mp4";
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState)
  13. {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_mymediaplayer_main);
  16.  
  17. vidView = (VideoView) findViewById(R.id.myVideo);
  18. vidControl = new MediaController(this);
  19. vidControl.setAnchorView(vidView);
  20. vidView.setMediaController(vidControl);
  21.  
  22. Uri vidUri = Uri.parse(vidAddress);
  23. vidView.setVideoURI(vidUri);
  24. vidView.start();
  25. }
  26.  
  27. }
  28.