android surfaceView 비디오 재생 기능 구현
9483 단어 androidsurfaceView비디오 재생
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);//
teAllTiem.setText(duration);//
teNowTiem.setText(currentTime);//
}
});
}
},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);
//
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();
}
});
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.