Mobile phone audio and video 4-basic functions of video players (1) and 4-basic functions
1. MediaPlayer and VideoView Introduction
The Android system provides developers to develop multimedia applications (audio and video)
1. MediaPlayer,
The underlying layer is decoded, And the MediaPlayer is responsible for dealing with the underlying layer. It encapsulates many methods such as start, pause, stop, and video playing.
This MediaPlayer requires the network-connected permission when it can play audio and video resources locally and on the network.
1. Execute the process
2. Videos in mp4, 3gp, And. m3u8 formats are directly used with the pc.mp4 file.
Ii. VideoView
Displays the video, inherits the SurfaceView class, implements the MediaPlayerControl interface, encapsulates MediaPlayer start, pause, stop, essentially calling MediaPlayer
SurfaceView video principles and stories
SurfaceView uses the Double Buffer technology by default. It supports Image Rendering in sub-threads, so it will not block the main thread, so it is more suitable for development of games and video players.
Implements the MediaPlayerControl interface to facilitate the control panel to call the VideoView Method
public interface MediaPlayerControl { void start(); void pause(); int getDuration(); int getCurrentPosition(); void seekTo(int pos); boolean isPlaying(); int getBufferPercentage(); boolean canPause(); boolean canSeekBackward(); boolean canSeekForward(); /** * Get the audio session id for the player used by this VideoView. This can be used to * apply audio effects to the audio track of a video. * @return The audio session, or 0 if there was an error. */ int getAudioSessionId(); }
Videoview. setMediaController (new MediaController (this ));
2. Player control bar top
1 <LinearLayout 2 android: id = "@ + id/ll_top" 3 android: layout_width = "match_parent" 4 android: layout_height = "wrap_content" 5 android: orientation = "vertical"> 6 7 <LinearLayout 8 android: layout_width = "match_parent" 9 android: layout_height = "wrap_content" 10 android: background = "@ drawable/bg_player_status" 11 android: gravity = "center_vertical" 12 android: orientation = "horizontal"> 13 <TextView14 android: id = "@ + id/TV _name" 15 android: layout_width = "wrap_content" 16 android: layout_height = "wrap_content" 17 android: layout_marginLeft = "8dp" 18 android: layout_weight = "1" 19 android: text = "video name" 20 android: textColor = "# ffffff"/> 21 22 <ImageView23 android: id = "@ + id/iv_battery" 24 android: layout_width = "wrap_content" 25 android: layout_height = "wrap_content" 26 android: layout_marginRight = "8dp" 27 android: src = "@ drawable/ic_battery_10"/> 28 29 <TextView30 android: id = "@ + id/TV _system_time" 31 android: layout_width = "wrap_content" 32 android: layout_height = "wrap_content" 33 android: layout_marginLeft = "8dp" 34 android: layout_marginRight = "8dp" 35 android: text = "12:00" 36 android: textColor = "# ffffff"/> 37 </LinearLayout> 38 39 <LinearLayout40 android: layout_width = "match_parent" 41 android: layout_height = "wrap_content" 42 android: background = "@ drawable/bg_player_top_control" 43 android: gravity = "center_vertical" 44 android: orientation = "horizontal"> 45 <Button46 android: id = "@ + id/btn_voice" 47 android: layout_width = "wrap_content" 48 android: layout_height = "wrap_content" 49 android: background = "@ drawable/btn_voice_selector"/> 50 51 <SeekBar52 android: id = "@ + id/seekbar_voice" 53 android: layout_width = "match_parent" 54 android: layout_height = "wrap_content" 55 android: layout_weight = "1" 56 android: maxHeight = "6dp" 57 android: minHeight = "6dp" 58 android: progressDrawable = "@ drawable/progress_horizontal" 59 android: thumb = "@ drawable/progress_thumb"/> 60 61 <Button62 android: id = "@ + id/btn_swich_player" 63 android: layout_width = "wrap_content" 64 android: layout_height = "wrap_content" 65 android: background = "@ drawable/btn_swich_player_selector"/> 66 </LinearLayout> 67 68 </LinearLayout>
View Code
3. Bottom of the player control bar
1 <LinearLayout 2 android:id="@+id/ll_bottom" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:layout_alignParentBottom="true" 6 android:orientation="vertical"> 7 8 <LinearLayout 9 android:layout_width="match_parent"10 android:layout_height="wrap_content"11 android:background="@drawable/bg_player_bottom_seekbar"12 android:gravity="center_vertical"13 android:orientation="horizontal">14 15 <TextView16 android:id="@+id/tv_current_time"17 android:layout_width="wrap_content"18 android:layout_height="wrap_content"19 android:layout_marginLeft="8dp"20 android:text="0:00"21 android:textColor="#ffffff" />22 23 <SeekBar24 android:id="@+id/seekbar_video"25 android:layout_width="match_parent"26 android:layout_height="wrap_content"27 android:layout_marginLeft="8dp"28 android:layout_marginRight="8dp"29 android:layout_weight="1"30 android:maxHeight="6dp"31 android:minHeight="6dp"32 android:progressDrawable="@drawable/progress_horizontal"33 android:thumb="@drawable/progress_thumb" />34 35 <TextView36 android:id="@+id/tv_duration"37 android:layout_width="wrap_content"38 android:layout_height="wrap_content"39 android:layout_marginRight="8dp"40 android:text="20:00"41 android:textColor="#ffffff" />42 43 </LinearLayout>44 45 46 <LinearLayout47 android:layout_width="match_parent"48 android:layout_height="wrap_content"49 android:background="@drawable/bg_player_bottom_control"50 android:gravity="center_vertical"51 android:orientation="horizontal">52 53 <Button54 android:id="@+id/btn_exit"55 android:layout_width="wrap_content"56 android:layout_height="wrap_content"57 android:layout_weight="1"58 android:background="@drawable/btn_exit_selector" />59 60 <Button61 android:id="@+id/btn_video_pre"62 android:layout_width="wrap_content"63 android:layout_height="wrap_content"64 android:layout_weight="1"65 android:background="@drawable/btn_video_pre_selector" />66 67 <Button68 android:id="@+id/btn_video_start_pause"69 android:layout_width="wrap_content"70 android:layout_height="wrap_content"71 android:layout_weight="1"72 android:background="@drawable/btn_video_pause_selector" />73 74 <Button75 android:id="@+id/btn_video_next"76 android:layout_width="wrap_content"77 android:layout_height="wrap_content"78 android:layout_weight="1"79 android:background="@drawable/btn_video_next_selector" />80 81 <Button82 android:id="@+id/btn_video_siwch_screen"83 android:layout_width="wrap_content"84 android:layout_height="wrap_content"85 android:layout_weight="1"86 android:background="@drawable/btn_video_siwch_screen_full_selector" />87 </LinearLayout>88 89 </LinearLayout>
View Code
4. Video seekBar Progress Update
1. The total length of the video and the setMaxt (total length) of the SeekBar. Note: After the callback is ready
2. instantiate Handler to get the current video playback progress every second, SeekBar. setProgress (current progress );
Int duration = videoview. getDuration (); // converts milliseconds. 40tv_duration.setText (utils. stringForTime (duration); // set the total length of the video to video_seekBar and equals video_seekBar.setMax (duration); // start updating handler when sending messages. sendEmptyMessage (PROGRESS );
5. Video dragging
Video_seekBar.setOnSeekBarChangeListener (newOnSeekBarChangeListener () {// callback when the finger stops sliding @ Override public void onStopTrackingTouch (SeekBar seekBar) {} // callback @ Override public void onStartTrackingTouch (SeekBar seekBar) when the finger is sliding) {}// callback @ Override public void onProgressChanged (SeekBar seekBar, int progress, booleanfromUser) {if (fromUser) {// seekBar. setProgress (progress); videoview. seekTo (progress );}}});