課程
/移動開發(fā)
/Android
/Android視頻播放器
求基礎源碼
2018-03-31
源自:Android視頻播放器 3-3
正在回答
package com.example.mp4;import java.io.IOException;
import com.example.mp4.R;
import android.media.AudioManager;import android.media.MediaPlayer;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.MediaController;import android.widget.SeekBar;import android.widget.TextView;import android.widget.VideoView;
import android.app.Activity;import android.content.res.Configuration;import android.os.Bundle;import android.widget.Button;
public class MainActivity extends Activity{?private VideoView videoView;?private LinearLayout controllerLayout;?private ImageView play_controller_img,screen_img;?private TextView current_text_tv,time_total_tv;?private SeekBar play_seek,volume_seek;?public static final int UPDATE_UI=1;?private AudioManager mAudioManager;?//定義音量控制?/**? * 定義videoview? */?@Override?protected void onCreate(Bundle savedInstanceState) {??super.onCreate(savedInstanceState);??setContentView(R.layout.activity_main);??mAudioManager=(AudioManager) getSystemService(AUDIO_SERVICE);//綁定,獲取系統(tǒng)的音頻服務??initUI();??setPlayerEvent();????String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/1.mp4";??/**?? * 本地視頻播放?? */??videoView.setVideoPath(path);??videoView.start();??UIHandler.sendEmptyMessage(UPDATE_UI);????/**?? *使MediaController控制視頻播放?? */?//?MediaController controller=new MediaController(this);??/**?? * 設置ViedoView與MediaController建立關聯(lián)?? */?//?videoView.setMediaController(controller);??/**?? * 設置MedioController與VideoView建立關聯(lián)?? */?//?controller.setMediaPlayer(videoView);?}?private void updateTextViewWithTimeFormat(TextView textview,int millisecond)?{??/**?? * 時間格式換化方法?? */???int second=millisecond/1000;??int hh=second/3600;??int mm=second/3600/60;??int ss=second%60;??String str=null;??if(hh!=0)??{???//format("")格式化字符變量???str=String.format("%02d:%02d:%02d",hh,mm,ss);??}??else{???str=String.format("%02d:%02d",mm,ss);??}??textview.setText(str);?}?private Handler UIHandler=new Handler(){??public void handleMessage(Message msg){???super.handleMessage(msg);???if(msg.what==UPDATE_UI){???//獲取視頻當前的播放時間???int currentPosition=videoView.getCurrentPosition();???//獲取視頻播放的總時間???int totalduration=videoView.getDuration();???//格式化視頻播放時間???updateTextViewWithTimeFormat(current_text_tv, currentPosition);???updateTextViewWithTimeFormat(time_total_tv,totalduration);???play_seek.setMax(totalduration);???play_seek.setProgress(currentPosition);???UIHandler.sendEmptyMessageDelayed(UPDATE_UI,500);???//每隔0.5秒就更新UI???}??}?};?protected void onPause() {??super.onPause();??UIHandler.removeMessages(UPDATE_UI);?}?@Override?protected void onDestroy() {??// TODO 自動生成的方法存根??super.onDestroy();?}?private void setPlayerEvent() {??/**?? * 控制視頻的播放和暫停的監(jiān)聽?? */??play_controller_img.setOnClickListener(new View.OnClickListener() {??????@Override???public void onClick(View v) {????// TODO 自動生成的方法存根????if(videoView.isPlaying()){?????play_controller_img.setImageResource(R.drawable.pause_btn_style);?????//暫停播放?????videoView.pause();?????UIHandler.removeMessages(UPDATE_UI);?????????}????else????{?????play_controller_img.setImageResource(R.drawable.pause_btn_style);?????//繼續(xù)播放?????videoView.start();?????UIHandler.sendEmptyMessage(UPDATE_UI);????}???}??});??play_seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
???@Override???public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {????// TODO 自動生成的方法存根????//根據(jù)當前進度刷新TextView的值????updateTextViewWithTimeFormat(current_text_tv,progress);???}
???@Override???public void onStartTrackingTouch(SeekBar seekBar) {????// TODO 自動生成的方法存根????UIHandler.removeMessages(UPDATE_UI);???}
???@Override???public void onStopTrackingTouch(SeekBar seekBar) {????// TODO 自動生成的方法存根????int progress=seekBar.getProgress();????//令視頻播放季度遵循seekBar停止拖動的這一刻進度????videoView.seekTo(progress);????//讓進度重新顯示????UIHandler.sendEmptyMessage(UPDATE_UI);????//???}????????});????volume_seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
??public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {???// TODO 自動生成的方法存根???//設置當前設備的音量???mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);??}
??public void onStartTrackingTouch(SeekBar seekBar) {???// TODO 自動生成的方法存根?????}
??public void onStopTrackingTouch(SeekBar seekBar) {???// TODO 自動生成的方法存根????}?????});?}?/**? * 初始化UI布局? */?private void initUI() {??// TODO 自動生成的方法存根??videoView=(VideoView) findViewById(R.id.videoView);??controllerLayout=(LinearLayout) findViewById(R.id.controllerbar_layout);??play_controller_img=(ImageView) findViewById(R.id.pause_img);??screen_img=(ImageView) findViewById(R.id.screen_img);??current_text_tv=(TextView) findViewById(R.id.time_current_tv);??time_total_tv=(TextView) findViewById(R.id.time_total_tv);??play_seek=(SeekBar) findViewById(R.id.play_seek);??volume_seek=(SeekBar) findViewById(R.id.volume_seek);??/**?? * 當前設備的最大音量?? */????int streamMaxVolume=mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);??/**?? * 獲取設備的最大音量?? */??int streamVolum=mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);??volume_seek.setMax(streamMaxVolume);??volume_seek.setProgress(streamVolum);?}?/**? *監(jiān)聽屏幕方向的改變??? ? */??????@Override?public void onConfigurationChanged(Configuration newConfig) {??// TODO 自動生成的方法存根??super.onConfigurationChanged(newConfig);?}?}
<?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"??? android:paddingBottom="@dimen/activity_vertical_margin"??? android:paddingLeft="@dimen/activity_horizontal_margin"??? android:paddingRight="@dimen/activity_horizontal_margin"??? android:paddingTop="@dimen/activity_vertical_margin"??? tools:context=".MainActivity" >
??? <VideoView ????? ??????? android:id="@+id/videoView"??????? android:layout_width="match_parent"??????? android:layout_height="match_parent"??????? />???? <LinearLayout ??????? android:id="@+id/controllerbar_layout"???????? android:orientation="vertical"???????? android:layout_alignParentBottom="true"????? android:layout_width="match_parent"???????? android:layout_height="50dp">???????? <SeekBar???????? android:id="@+id/play_seek"???????? android:thumb="@null"??? ???????? android:max="100"???????? android:progress="20"???????? android:progressDrawable="@drawable/seekbar_style2" ???????? android:indeterminate="false"???????? android:layout_marginLeft="20dp"???????? android:layout_marginRight="20dp"? ????? android:layout_width="match_parent"???????? android:layout_height="5dp" />???????? <RelativeLayout??????????? android:layout_width="wrap_content"??????????? android:layout_height="match_parent"??????????? android:background="#000"??????????? android:gravity="center_vertical" >???????????? ???? <ImageView ????????? android:id="@+id/pause_img"????????? android:layout_marginLeft="6dp"????????? android:src="@drawable/pause_btn_style"????????? android:layout_width="wrap_content"????????? android:layout_height="wrap_content"/>????? <TextView ????????? android:id="@+id/time_current_tv"????????? android:layout_width="wrap_content"????????? android:layout_height="wrap_content"????????? android:text="00:00:00"????????? android:textColor="#ffffff"????????? android:textSize="10sp"????????? android:layout_marginTop="12dp"????????? android:layout_marginLeft="32dp"/>??????? <TextView ????????? android:layout_width="wrap_content"????????? android:layout_height="wrap_content"????????? android:text="/"????????? android:textColor="#4c4c4c"????????? android:textSize="10sp"??????????? android:layout_marginTop="12dp"????????? android:layout_marginLeft="70dp"/>????????? <TextView ????????? android:id="@+id/time_total_tv"????????? android:layout_width="wrap_content"????????? android:layout_height="wrap_content"????????? android:text="00:00:00"????????? android:textColor="#4c4c4c"????????? android:textSize="10sp"??????????? android:layout_marginTop="12dp"????????? android:layout_marginLeft="75dp"/>????????? ????????? <LinearLayout ?????????? ??????????? android:id="@+id/left_layout"??????????? android:layout_width="150dp"??????????? android:layout_height="50dp"???????????? android:orientation="vertical"??????????? android:layout_alignParentRight="true">
??????????? <ImageView??????????????? android:layout_width="wrap_content"??????????????? android:layout_height="wrap_content"??????????????? android:src="@drawable/volumn" />
??????????? <SeekBar??????????????? android:id="@+id/volume_seek"??????????????? android:layout_width="100dp"??????????????? android:layout_height="5dp"??????????????? android:layout_marginLeft="40dp"??????????????? android:layout_marginTop="-15dp"??????????????? android:max="100"??????????????? android:progress="20"??????????????? android:progressDrawable="@drawable/seekbar_style"??????????????? android:thumb="@null" />?<ImageView ????? ????? android:id="@+id/screen_img"?????? ???? android:src="@drawable/screen_img"?????? ???? android:layout_marginLeft="130dp"?????? ????? android:layout_width="20dp"?????? ????? android:layout_height="25dp"/>??????? ?</LinearLayout>?????????? </RelativeLayout>
</LinearLayout></RelativeLayout>??
舉報
Android系統(tǒng)自帶的以及自定義播放器
1 回答求視頻或者音樂播放器源代碼
1 回答視頻播放問題
4 回答視頻無法播放
1 回答無法播放視頻
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2018-12-18
package com.example.mp4;
import java.io.IOException;
import com.example.mp4.R;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends Activity{
?private VideoView videoView;
?private LinearLayout controllerLayout;
?private ImageView play_controller_img,screen_img;
?private TextView current_text_tv,time_total_tv;
?private SeekBar play_seek,volume_seek;
?public static final int UPDATE_UI=1;
?private AudioManager mAudioManager;
?//定義音量控制
?/**
? * 定義videoview
? */
?@Override
?protected void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.activity_main);
??mAudioManager=(AudioManager) getSystemService(AUDIO_SERVICE);//綁定,獲取系統(tǒng)的音頻服務
??initUI();
??setPlayerEvent();
??
??String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/1.mp4";
??/**
?? * 本地視頻播放
?? */
??videoView.setVideoPath(path);
??videoView.start();
??UIHandler.sendEmptyMessage(UPDATE_UI);
??
??/**
?? *使MediaController控制視頻播放
?? */
?//?MediaController controller=new MediaController(this);
??/**
?? * 設置ViedoView與MediaController建立關聯(lián)
?? */
?//?videoView.setMediaController(controller);
??/**
?? * 設置MedioController與VideoView建立關聯(lián)
?? */
?//?controller.setMediaPlayer(videoView);
?}
?private void updateTextViewWithTimeFormat(TextView textview,int millisecond)
?{
??/**
?? * 時間格式換化方法
?? */
?
??int second=millisecond/1000;
??int hh=second/3600;
??int mm=second/3600/60;
??int ss=second%60;
??String str=null;
??if(hh!=0)
??{
???//format("")格式化字符變量
???str=String.format("%02d:%02d:%02d",hh,mm,ss);
??}
??else{
???str=String.format("%02d:%02d",mm,ss);
??}
??textview.setText(str);
?}
?private Handler UIHandler=new Handler(){
??public void handleMessage(Message msg){
???super.handleMessage(msg);
???if(msg.what==UPDATE_UI){
???//獲取視頻當前的播放時間
???int currentPosition=videoView.getCurrentPosition();
???//獲取視頻播放的總時間
???int totalduration=videoView.getDuration();
???//格式化視頻播放時間
???updateTextViewWithTimeFormat(current_text_tv, currentPosition);
???updateTextViewWithTimeFormat(time_total_tv,totalduration);
???play_seek.setMax(totalduration);
???play_seek.setProgress(currentPosition);
???UIHandler.sendEmptyMessageDelayed(UPDATE_UI,500);
???//每隔0.5秒就更新UI
???}
??}
?};
?protected void onPause() {
??super.onPause();
??UIHandler.removeMessages(UPDATE_UI);
?}
?@Override
?protected void onDestroy() {
??// TODO 自動生成的方法存根
??super.onDestroy();
?}
?private void setPlayerEvent() {
??/**
?? * 控制視頻的播放和暫停的監(jiān)聽
?? */
??play_controller_img.setOnClickListener(new View.OnClickListener() {
???
???@Override
???public void onClick(View v) {
????// TODO 自動生成的方法存根
????if(videoView.isPlaying()){
?????play_controller_img.setImageResource(R.drawable.pause_btn_style);
?????//暫停播放
?????videoView.pause();
?????UIHandler.removeMessages(UPDATE_UI);
?????
????}
????else
????{
?????play_controller_img.setImageResource(R.drawable.pause_btn_style);
?????//繼續(xù)播放
?????videoView.start();
?????UIHandler.sendEmptyMessage(UPDATE_UI);
????}
???}
??});
??play_seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
???@Override
???public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
????// TODO 自動生成的方法存根
????//根據(jù)當前進度刷新TextView的值
????updateTextViewWithTimeFormat(current_text_tv,progress);
???}
???@Override
???public void onStartTrackingTouch(SeekBar seekBar) {
????// TODO 自動生成的方法存根
????UIHandler.removeMessages(UPDATE_UI);
???}
???@Override
???public void onStopTrackingTouch(SeekBar seekBar) {
????// TODO 自動生成的方法存根
????int progress=seekBar.getProgress();
????//令視頻播放季度遵循seekBar停止拖動的這一刻進度
????videoView.seekTo(progress);
????//讓進度重新顯示
????UIHandler.sendEmptyMessage(UPDATE_UI);
????//
???}
???
???
??});
??
?
?volume_seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
??public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
???// TODO 自動生成的方法存根
???//設置當前設備的音量
???mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
??}
??public void onStartTrackingTouch(SeekBar seekBar) {
???// TODO 自動生成的方法存根
???
??}
??public void onStopTrackingTouch(SeekBar seekBar) {
???// TODO 自動生成的方法存根
??
??}
??
??
?});
?
}
?/**
? * 初始化UI布局
? */
?private void initUI() {
??// TODO 自動生成的方法存根
??videoView=(VideoView) findViewById(R.id.videoView);
??controllerLayout=(LinearLayout) findViewById(R.id.controllerbar_layout);
??play_controller_img=(ImageView) findViewById(R.id.pause_img);
??screen_img=(ImageView) findViewById(R.id.screen_img);
??current_text_tv=(TextView) findViewById(R.id.time_current_tv);
??time_total_tv=(TextView) findViewById(R.id.time_total_tv);
??play_seek=(SeekBar) findViewById(R.id.play_seek);
??volume_seek=(SeekBar) findViewById(R.id.volume_seek);
??/**
?? * 當前設備的最大音量
?? */
??
??int streamMaxVolume=mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
??/**
?? * 獲取設備的最大音量
?? */
??int streamVolum=mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
??volume_seek.setMax(streamMaxVolume);
??volume_seek.setProgress(streamVolum);
?}
?/**
? *監(jiān)聽屏幕方向的改變???
? */
??
??
?
?@Override
?public void onConfigurationChanged(Configuration newConfig) {
??// TODO 自動生成的方法存根
??super.onConfigurationChanged(newConfig);
?}
?
}
<?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"
??? android:paddingBottom="@dimen/activity_vertical_margin"
??? android:paddingLeft="@dimen/activity_horizontal_margin"
??? android:paddingRight="@dimen/activity_horizontal_margin"
??? android:paddingTop="@dimen/activity_vertical_margin"
??? tools:context=".MainActivity" >
??? <VideoView
?????
??????? android:id="@+id/videoView"
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? />
???? <LinearLayout
??????? android:id="@+id/controllerbar_layout"
???????? android:orientation="vertical"
???????? android:layout_alignParentBottom="true"
????? android:layout_width="match_parent"
???????? android:layout_height="50dp">
???????? <SeekBar
???????? android:id="@+id/play_seek"
???????? android:thumb="@null"???
???????? android:max="100"
???????? android:progress="20"
???????? android:progressDrawable="@drawable/seekbar_style2"
???????? android:indeterminate="false"
???????? android:layout_marginLeft="20dp"
???????? android:layout_marginRight="20dp"?
????? android:layout_width="match_parent"
???????? android:layout_height="5dp" />
???????? <RelativeLayout
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:background="#000"
??????????? android:gravity="center_vertical" >
???????????? ???? <ImageView
????????? android:id="@+id/pause_img"
????????? android:layout_marginLeft="6dp"
????????? android:src="@drawable/pause_btn_style"
????????? android:layout_width="wrap_content"
????????? android:layout_height="wrap_content"/>
????? <TextView
????????? android:id="@+id/time_current_tv"
????????? android:layout_width="wrap_content"
????????? android:layout_height="wrap_content"
????????? android:text="00:00:00"
????????? android:textColor="#ffffff"
????????? android:textSize="10sp"
????????? android:layout_marginTop="12dp"
????????? android:layout_marginLeft="32dp"/>
??????? <TextView
????????? android:layout_width="wrap_content"
????????? android:layout_height="wrap_content"
????????? android:text="/"
????????? android:textColor="#4c4c4c"
????????? android:textSize="10sp"
??????????? android:layout_marginTop="12dp"
????????? android:layout_marginLeft="70dp"/>
????????? <TextView
????????? android:id="@+id/time_total_tv"
????????? android:layout_width="wrap_content"
????????? android:layout_height="wrap_content"
????????? android:text="00:00:00"
????????? android:textColor="#4c4c4c"
????????? android:textSize="10sp"
??????????? android:layout_marginTop="12dp"
????????? android:layout_marginLeft="75dp"/>
?????????
????????? <LinearLayout
??????????
??????????? android:id="@+id/left_layout"
??????????? android:layout_width="150dp"
??????????? android:layout_height="50dp"
???????????? android:orientation="vertical"
??????????? android:layout_alignParentRight="true">
??????????? <ImageView
??????????????? android:layout_width="wrap_content"
??????????????? android:layout_height="wrap_content"
??????????????? android:src="@drawable/volumn" />
??????????? <SeekBar
??????????????? android:id="@+id/volume_seek"
??????????????? android:layout_width="100dp"
??????????????? android:layout_height="5dp"
??????????????? android:layout_marginLeft="40dp"
??????????????? android:layout_marginTop="-15dp"
??????????????? android:max="100"
??????????????? android:progress="20"
??????????????? android:progressDrawable="@drawable/seekbar_style"
??????????????? android:thumb="@null" />
?<ImageView
????? ????? android:id="@+id/screen_img"
?????? ???? android:src="@drawable/screen_img"
?????? ???? android:layout_marginLeft="130dp"
?????? ????? android:layout_width="20dp"
?????? ????? android:layout_height="25dp"/>
??????? ?</LinearLayout>
??
???????? </RelativeLayout>
</LinearLayout></RelativeLayout>
?
?