android surfaceView實現(xiàn)播放視頻功能
本文實例為大家分享了android surfaceView實現(xiàn)播放視頻的具體代碼,供大家參考,具體內(nèi)容如下
RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.examday11_4_1.surfaceview.MySurfaceView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="250dp" />
<TextView
android:id="@+id/te_nowTiem"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/sv"
android:layout_marginLeft="60dp"
android:layout_marginBottom="10dp"
android:text="00:00" />
<SeekBar
android:id="@+id/sb"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/sv"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" />
<TextView
android:id="@+id/te_allTiem"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/sv"
android:layout_marginLeft="320dp"
android:layout_marginBottom="10dp"
android:text="00:00" />
<Button
android:id="@+id/but_play"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_alignBottom="@+id/sv"
android:layout_marginLeft="10dp"
android:text="S/P"
android:textSize="10sp" />
</RelativeLayout>
mySurfaceView
package com.example.examday11_4_1.surfaceview;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder surfaceHolder;
private MediaPlayer mediaPlayer;
public MySurfaceView(Context context) {
super(context);
}
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
if (mediaPlayer == null){
mediaPlayer = new MediaPlayer();
}
}
public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setDataPath(String path){
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(surfaceHolder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mediaPlayer!=null){
mediaPlayer.release();
mediaPlayer = null;
}
}
//暫停/開始播放
public void playOrNo(){
if (mediaPlayer!=null){
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
}else {
mediaPlayer.start();
}
}
}
//拖動更新進度
public void seekTo(int progress){
if (mediaPlayer!=null){
int duration = mediaPlayer.getDuration();
int current = progress * duration /100;
mediaPlayer.seekTo(current);
}
}
//獲取播放進度
public int getProgress(){
if (mediaPlayer!=null){
int druation = mediaPlayer.getDuration();
int currentPosition = mediaPlayer.getCurrentPosition();
int progress = currentPosition * 100 / druation;
return progress;
}
return 0;
}
//獲取播放時長
public String getCurrentTime(){
if (mediaPlayer!=null){
long currentPostion = mediaPlayer.getCurrentPosition();
SimpleDateFormat format = new SimpleDateFormat("mm:ss");
String f = format.format(currentPostion);
return f+"";
}
return "";
}
//獲取時長
public String getDuration(){
if (mediaPlayer!=null){
long duration = mediaPlayer.getDuration();
SimpleDateFormat format = new SimpleDateFormat("mm:ss");
return format.format(duration)+"";
}
return "";
}
}
MainActivity
package com.example.examday11_4_1;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.examday11_4_1.surfaceview.MySurfaceView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private MySurfaceView mySurfaceView;
private TextView teNowTiem;
private SeekBar sb;
private TextView teAllTiem;
private Button butPlay;
private Timer timer;
private Handler handler = new Handler();
private String path ="http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE
},100);
}
initView();
initTimer();
}
private void initTimer() {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
final String currentTime = mySurfaceView.getCurrentTime();
final String duration = mySurfaceView.getDuration();
final int progress = mySurfaceView.getProgress();
handler.post(new Runnable() {
@Override
public void run() {
sb.setProgress(progress);//設(shè)置進度條
teAllTiem.setText(duration);//設(shè)置總時長
teNowTiem.setText(currentTime);//設(shè)置當前時長
}
});
}
},0,100);
}
private void initView() {
mySurfaceView = (MySurfaceView) findViewById(R.id.sv);
mySurfaceView.setDataPath(path);
teNowTiem = (TextView) findViewById(R.id.te_nowTiem);
sb = (SeekBar) findViewById(R.id.sb);
teAllTiem = (TextView) findViewById(R.id.te_allTiem);
butPlay = (Button) findViewById(R.id.but_play);
//設(shè)置拖動
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser){
mySurfaceView.seekTo(progress);//視頻播放拖動
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//暫停播放
butPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mySurfaceView.playOrNo();
}
});
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio 3.6 正式版終于發(fā)布了,快來圍觀
Android Studio 3.6 正式版終于發(fā)布了,值得興奮呀,畢竟 3.5 大版本更新也已經(jīng)差不多半年了,撒花撒花!這次更新又更新了什么呢?快來跟隨小編一起看看吧2020-02-02
完美解決客戶端webview持有的頁面緩存,不會立即釋放的問題
下面小編就為大家?guī)硪黄昝澜鉀Q客戶端webview持有的頁面緩存,不會立即釋放的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
Android音視頻開發(fā)之MediaExtactor使用教程
MediaExtactor在Android音視頻開發(fā)中負責提取音視頻信息和數(shù)據(jù)流的功能,可以通過該類實現(xiàn)從多媒體文件中剝離得到音頻和視頻的能力。本文將詳細為大家介紹一下它的使用,感興趣的可以了解一下2022-04-04
解決Eclipse啟動出錯:Failed to create the Java Virtual Machine
這篇文章主要介紹了解決Eclipse啟動出錯:Failed to create the Java Virtual Machine的相關(guān)資料,這里說明出錯原因及查找錯誤和解決辦法,需要的朋友可以參考下2017-07-07
Android 使用mediaplayer播放res/raw文件夾中的音樂的實例
這篇文章主要介紹了Android 使用mediaplayer播放res/raw文件夾中的音樂的實例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android實現(xiàn)濾鏡效果ColorMatrix
這篇文章主要為大家詳細介紹了Android實現(xiàn)濾鏡效果ColorMatrix,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
Android編程實現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法
這篇文章主要介紹了Android編程實現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法,涉及Android屏幕分辨率、布局、橫豎屏切換等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12

