欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

视频.MP4使用 SurfaceView一个小视频的播放

程序员文章站 2022-07-13 12:08:39
...

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".Video">

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="400dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">
        <ImageView
            android:id="@+id/tui"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aau"
            android:layout_toLeftOf="@+id/center"
            />
        <ImageView
            android:id="@+id/center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bhc"
            android:layout_centerHorizontal="true"
            />
        <ImageView
            android:id="@+id/qian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aaq"
            android:layout_toRightOf="@+id/center"
            />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="35dp">
        <TextView
            android:id="@+id/t1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentLeft="true"
            />
        <SeekBar
            android:id="@+id/seek"
            android:layout_width="290dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/t1"
            />
        <TextView
            android:id="@+id/t2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="55"
            android:layout_toRightOf="@+id/seek"
            />
    </RelativeLayout>


</LinearLayout>

Activity文件

package com.example.study_seven;

import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class Video extends AppCompatActivity implements SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener, View.OnClickListener {

SurfaceView surfaceView;
MediaPlayer mediaPlayer;
SurfaceHolder surfaceHolder;
SeekBar seekBar;
boolean flagStrat;
TextView nowText;
TextView countText;
StringBuilder sb;
int duration;
int currentPosition;
ImageView kuai;
ImageView hou;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);

    init();

    surfaceHolder=surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    findViewById(R.id.center).setOnClickListener(this);
    seekBar.setOnSeekBarChangeListener(this);


}

private void init() {
    surfaceView=findViewById(R.id.surface);
    seekBar=findViewById(R.id.seek);
    nowText=findViewById(R.id.t1);
    countText=findViewById(R.id.t2);
    kuai=findViewById(R.id.qian);
    hou=findViewById(R.id.tui);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mediaPlayer=new MediaPlayer();

// Uri uri=Uri.parse("/mnt/sdcard/123.mp4");
try {
// mediaPlayer.setDataSource(this,uri);
mediaPlayer.setDataSource("/mnt/sdcard/123.mp4");
} catch (IOException e) {
e.printStackTrace();
}

    try {
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.setDisplay(surfaceHolder);
    qian_Tui();
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
            flagStrat=true;
             duration= mediaPlayer.getDuration();
            int second=(int)duration/1000/60;
            int minutes=(int)duration/1000%60;
            int millminutes=(int)duration/1000%60/60;
            countText.setText(second+"."+minutes+""+millminutes);
            seekBar.setMax(duration);
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                   currentPosition = mediaPlayer.getCurrentPosition();

                    seekBar.setProgress(currentPosition);
                    int second=(int)currentPosition/1000/60;
                    int minutes=(int)currentPosition/1000%60;
                    int millminutes=(int)currentPosition/1000%60/60;
                    sb=new StringBuilder();
                    sb.append(second+"."+minutes+""+millminutes);

// nowText.setText(second+"."+minutes+""+millminutes);
Log.e("##",second+"."+minutes+""+millminutes);

                }
            },0,100);


        }

    });



}

private void qian_Tui() {
        kuai.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int currentPosition = mediaPlayer.getCurrentPosition();
            mediaPlayer.seekTo(currentPosition+15000);

        }
    });
    hou.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int currentPosition = mediaPlayer.getCurrentPosition();
            mediaPlayer.seekTo(currentPosition-15000);

        }
    });
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}




@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    int progress = seekBar.getProgress();
    mediaPlayer.seekTo(progress);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.center:
            if(!flagStrat) {
                mediaPlayer.start();
                findViewById(R.id.center).setBackgroundResource(R.drawable.bhc);
                flagStrat=true;
            }else{
                mediaPlayer.pause();
                findViewById(R.id.center).setBackgroundResource(R.drawable.bhl);
                flagStrat=false;
            }
            break;
    }
}

}

效果

视频.MP4使用 SurfaceView一个小视频的播放