SurfaceView和MediaPlayer

SurfaceView和MediaPlayer

一、效果

在这里插入图片描述

二、代码示例

//布局xml文件
<?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">

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom"/>

    <RelativeLayout
        android:id="@+id/bottom"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <CheckBox
            android:id="@+id/playorpause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/button_play"
            android:layout_centerInParent="true"/>
        <ImageView
            android:id="@+id/privous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/privous"
            android:layout_marginRight="30dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/playorpause"/>
        <ImageView
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@drawable/next"
            android:layout_toRightOf="@id/playorpause"
            android:layout_marginLeft="30dp"/>

        <LinearLayout
            android:layout_below="@id/playorpause"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/nowTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="00:00"
                android:textSize="20sp"/>
            <SeekBar
                android:id="@+id/seekBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center"/>
            <TextView
                android:id="@+id/endTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text="01:20"/>

        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>
//java代码
package day01.bw.com.day0612homework;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
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.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, SeekBar.OnSeekBarChangeListener {

    private MediaPlayer player;
    private CheckBox playorpause;
    private ImageView privous;
    private ImageView next;
    private TextView nowTime;
    private TextView endTime;
    private SeekBar seekBar;
    private boolean isPlaying;
    private boolean isPause;
    private SurfaceView surfaceView;
    private SurfaceHolder holder;
    private int time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
//        initMediaPlayer();
    }

    private void initMediaPlayer() {
        player = new MediaPlayer();

        try {
//            player.setDataSource("/sdcard/mpp.mp4");
            Uri uri = Uri.parse("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4");
            player.setDataSource(this,uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                player.setLooping(true);
                int duration = player.getDuration();
                seekBar.setMax(duration);
                String second = duration/1000%60+"";
                if(second.length()==1){
                    second = "0"+second;
                }
                String minute = duration/1000/60+"";
                if(minute.length()==1){
                    minute = "0"+minute;
                }
                endTime.setText(minute+":"+second);
                player.start();
                Message message = Message.obtain();
                message.arg1=time;
                handler.sendMessage(message);
                isPlaying=true;
            }
        });

    }

    private void init() {
        playorpause = findViewById(R.id.playorpause);
        privous = findViewById(R.id.privous);
        next = findViewById(R.id.next);
        nowTime = findViewById(R.id.nowTime);
        endTime = findViewById(R.id.endTime);
        seekBar = findViewById(R.id.seekBar);
        surfaceView = findViewById(R.id.surfaceView);
        holder = surfaceView.getHolder();
        holder.addCallback(this);
        seekBar.setOnSeekBarChangeListener(this);

        playorpause.setChecked(true);
        playorpause.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    if(isPause){
                        player.start();
                        openHandler();
                    }

                }else{
                    if(isPlaying){
                        player.pause();
                        handler.removeCallbacksAndMessages(null);
                        isPause=true;
                    }

                }
            }
        });
        privous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                time-=10;
                setNowTime(time);
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                time+=10;
                setNowTime(time);
            }
        });
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            seekBar.setProgress(time * 1000);
            Message message = Message.obtain();
            time++;
            setNowTime(time);
            handler.sendMessageDelayed(message,1000);
        }
    };

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        initMediaPlayer();
        player.setDisplay(holder);
        player.prepareAsync();
    }

    @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) {
        openHandler();
    }
    private void openHandler(){
        player.seekTo(seekBar.getProgress());
        handler.removeCallbacksAndMessages(null);
        Message message = Message.obtain();
        time = (seekBar.getProgress() / 1000);
        setNowTime(time);
        handler.sendMessage(message);
    }
    private void setNowTime(int time){
        String second = time%60+"";
        if(second.length()==1){
            second = "0"+second;
        }
        String minute = time/60+"";
        if(minute.length()==1){
            minute = "0"+minute;
        }
        nowTime.setText(minute+":"+second);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值