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

布局文件

<?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;
    }
}

}

效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值