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

android surfaceView实现播放视频功能

程序员文章站 2023-11-06 17:32:40
本文实例为大家分享了android surfaceview实现播放视频的具体代码,供大家参考,具体内容如下relativelayout

本文实例为大家分享了android surfaceview实现播放视频的具体代码,供大家参考,具体内容如下

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();
      }
    });

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。