7th section Vertical screen of the playback interface
The ability to play the video is placed in a separate activity. We'll set up a two-screen layout for each of them.
In the vertical screen, the upper part of the video playback, the lower half of the video information;
When the device rotates Cheng, the video is played in full screen;
7.1 Start the video playback interface
When you click on a video item in the video list, the player plays the corresponding video. Here we are going to create a name VideoPlayer
for the activity and use it to complete the task of video playback.
In addition, to ListView
add a data item click on the Listener function,
- Implementation
ListView
of the OnItemClickListener
interface;
- Put the URI where you want to play the video address
Intent
;
- Start the activity-videoplayer of the video player by intent;
@OverridepublicvoidonItemClickintlong id) { VideoItem item = mVideoList.get(position); new Intent(this, VideoPlayer.class); i.setData(Uri.parse(item.path)); startActivity(i);}
Here is the Intent
bridge between the various components of the Android system that can,
- Wakes the specified component and lets it start running. For example, start activity B at activity A, or start a service A;
- Passing data to individual components;
7.2 Videoview
Play video You can use the ready-made controls provided by the Android SDK VideoView
. It is the right media player
and surface
the package, for the first time for the development of video playback of us, the use VideoView
is the simplest and convenient, do not pay attention to the implementation of too much detail.
VideoView
The use of, very simple,
Place a control in the layout file VideoView
,
<VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent"/>
In the activity, get the layout file VideoView
, let's set the video address to play,
mVideoView = (VideoView) findViewById(R.id.video_view);mVideoView.setVideoPath(path);
VideoView
control the flow of video playback using the provided interface,
//从头开始播放视频mVideoView.start();//暂停播放视频mVideoView.pause();//继续播放视频mVideoView.resume()//跳转到xxx毫秒处开始播放mVideoView.seekTo(xxx);
You can also VideoView
add Control Panel- MediaController
This panel integrates the play progress drag, pause, resume playback and other functions, but also can be automatically hidden or displayed. If VideoView
there is a parent layout, then it is added to the MediaController
bottom of the parent layout. Therefore, in order to be aesthetically pleasing, we will put them in VideoView
a separate layout file FrameLayout
.
Here we use the Android SDK comes with Media Controller
,
new MediaController(context);mVideoView.setMediaController(controller);
7.3 Vertical Screen Layout
We will design the vertical screen layout into two parts, the above with VideoView
video playback, the following with a TableLayout
display video information,
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout 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"android:orientation="Vertical" > <framelayout android: Layout_width = "match_parent" android:layout_ Height = "0DP" android:layout_weight =< Span class= "Hljs-value" > "1" android:layout_margin =" 10DP " android:background =" @ Color/video_background_color "; <videoviewandroid:layout_gravity="center"android:id="@+id/video _view "android:layout_width=" Match_parent "android:layout_height=" Match_ Parent "/> </framelayout> <tablelayoutandroid:layout_width="Match_parent"android:layout_height ="0DP"android:layout_weight="1"android:layout_margin="10DP" > <TableRow> <TextViewandroid:text="@string/video_name"/> <TextViewandroid:id="@+id/video_title"/> </TableRow> <TableRow> <TextViewandroid:text="@string/video_width_height"/> <TextViewandroid:id="@+id/video_width_height"/> </TableRow> <TableRow> <TextViewandroid:text="@string/video_size"/> <TextViewandroid:id="@+id/video_size"/> </TableRow> <TableRow> <TextViewandroid:text="@string/created_time"/> <TextViewandroid:id="@+id/video_create_time"/> </TableRow> </tablelayout></linearlayout>
This puts the content in the middle of the VideoView
FrameLayout
display, and sets the background color for the area.
7.4 Setting up Video information
By booting VideoPlayer
Intent
, get the video playback address,
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Uri uri = getIntent().getData(); String path = uri.getPath(); ......}
Find additional information about the video-video title, video file size, video size, video creation time-by playing the address.
@Override?protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); ...... Uri path = Uri.getpath (); string[] Searchkey =NewString[] {MediaStore.Video.Media.TITLE, MediaStore.Video.Media.WIDTH, MediaStore.Video.Media.HEIGHT, MediaStore.Images.Media.SIZE, mediastore.images . media.date_added}; String where = MediaStore.Video.Media.DATA +"= '"+ Path +"'"; string[] Keywords =NULL; String sortOrder = MediaStore.Video.Media.DEFAULT_SORT_ORDER; cursor cursor = getcontentresolver (). Query (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, Searchkey, where, keywords, SortOrder);if(Cursor! =NULL) {if(Cursor.getcount () >0) {Cursor.movetonext (); String createdtime = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATE_ADDED)); String name = cursor.getstring (Cursor.getcolumnindex (MediaStore.Video.Media.TITLE));intSize = Cursor.getint (Cursor.getcolumnindex (MediaStore.Video.Media.SIZE));intwidth = Cursor.getint (Cursor.getcolumnindex (MediaStore.Video.Media.WIDTH));intHeight = cursor.getint (cursor.getcolumnindex (MediaStore.Video.Media.HEIGHT)); Videoitem item =NewVideoitem (path, name, Createdtime); TextView title = (TextView) Findviewbyid (r.id.video_title); Title.settext (Item.name); TextView created = (TextView) Findviewbyid (r.id.video_create_time); Created.settext (Item.createdtime); TextView screen = (TextView) Findviewbyid (r.id.video_width_height); Screen.settext (width +"*"+ height); TextView fileSize = (TextView) Findviewbyid (r.id.video_size); Filesize.settext (string.valueof (Size/1024x768/1024x768) +"M"); }Else{TextView title = (TextView) Findviewbyid (r.id.video_title); Title.settext (R.string.unknown); TextView created = (TextView) Findviewbyid (r.id.video_create_time); Created.settext (R.string.unknown); TextView screen = (TextView) Findviewbyid (r.id.video_width_height); Screen.settext (R.string.unknown); TextView fileSize = (TextView) Findviewbyid (r.id.video_size); Filesize.settext (R.string.unknown); } cursor.close (); } ......}
7.5 Setting Playback
Gets VideoView
the control and VideoView
plays the video for the settings. Added the Android SDK Control Panel for the playback interface- MediaController
@Override?protectedvoidonCreate(Bundle savedInstanceState) {? super.onCreate(savedInstanceState); ...... String path = uri.getPath(); mVideoView = (VideoView) findViewById(R.id.video_view); mVideoView.setVideoPath(path); new MediaController(this); mVideoView.setMediaController(controller);}
7.6 Pause and resume of video
Sometimes the user needs to pause the video, which can be manipulated by the buttons on the control Panel, but sometimes the video being played is interrupted by the system, such as a phone call, the highest priority of the phone activity, which is displayed to pause the video player, or when the user presses the Home
button to switch to the home screen and pause the video playback. When these actions are finished, once again VideoPlayer
switch to the foreground, we want the video to continue to play from where it was just interrupted.
We've learned about the life cycle of activity,
Created from an activity, to the display, and then to the user's active exit to destroy the activity, it will experience:
onCreate()
-
onStart()
-
onResume()
-
User can interact with activity
onPause()
-
onStop()
-
onDestroy()
;
You can then select the action in which to onResume()
onPause()
pause and resume video playback.
In onPause()
, pause the playback of the video, record the current playing position, in onResume()
, from the last record of the location of the interrupted, start playback,
@OverrideprotectedvoidonPause() { super.onPause(); mVideoView.pause(); mLastPlayedTime = mVideoView.getCurrentPosition();}@OverrideprotectedvoidonResume() { super.onResume(); mVideoView.start(); if0) { mVideoView.seekTo(mLastPlayedTime); }}
Teach you to do video player (v)