成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

androidsurfaceView如何實現(xiàn)播放視頻功能

這篇文章主要為大家展示了android surfaceView如何實現(xiàn)播放視頻功能,內(nèi)容簡而易懂,希望大家可以學(xué)習一下,學(xué)習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比花山網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式花山網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋花山地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。

RelativeLayout

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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();
      }
    });

  }
}

以上就是關(guān)于android surfaceView如何實現(xiàn)播放視頻功能的內(nèi)容,如果你們有學(xué)習到知識或者技能,可以把它分享出去讓更多的人看到。

文章名稱:androidsurfaceView如何實現(xiàn)播放視頻功能
網(wǎng)頁URL:http://jinyejixie.com/article42/ijcoec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、虛擬主機網(wǎng)站導(dǎo)航、網(wǎng)站排名云服務(wù)器、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
乌拉特后旗| 姜堰市| 枣阳市| 西充县| 定安县| 苗栗县| 沅江市| 将乐县| 东光县| 阳原县| 龙陵县| 嘉禾县| 英超| 阿勒泰市| 雅江县| 宣城市| 射洪县| 浙江省| 潮安县| 酒泉市| 江源县| 邢台市| 湖口县| 蓬安县| 合作市| 临安市| 西峡县| 洪湖市| 卫辉市| 蕲春县| 永州市| 临颍县| 衡东县| 万年县| 黑水县| 菏泽市| 寿宁县| 勃利县| 宁化县| 玉溪市| 蓬莱市|