android使用MediaPlayer+Surface实现简单视频播放器

MediaPlayer主要用于播放音频,本身没有提供图像输出界面,此时需要借助于SurfaceView来显示MediaPlayer播放的图像输出。下面用一个简单示例来演示:
效果图没找见:就直接上代码吧。
Activity
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
private SurfaceView surfaceView;
private MediaPlayer player;
private SurfaceHolder holder;
private ProgressBar progressBar;
private SeekBar seekBar;
private Button submit;
private TextView allTimes;
private int times;
private Timer timer;
private TimerTask task;
private Thread th;
public boolean flag = true;
这个地址有可能失效
private String uri = “http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com/D046015255134077DDB3ACA0D7E68D45.flv“;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppTheme);//这里是我做的冷启动处理才会用到,不理解的可以不用加这句
    setContentView(R.layout.activity_main);
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    initView();
    player = new MediaPlayer();
    th = new MyThread();
    th.start();
    initPrepare();
    initOnclick();
    initSeekBar();
}

private void initOnclick() {
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (player.isPlaying()) {
                player.pause();
                submit.setText("START");
                progressBar.setVisibility(View.INVISIBLE);
            } else {
                player.start();
                submit.setText("PAUSE");
            }
        }
    });
}

private void initPrepare() {
    try {
        player.setDataSource(MainActivity.this, Uri.parse(uri));
        holder = surfaceView.getHolder();
        holder.addCallback(new MyCallBack());
        player.prepare();
        player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                if (mp.isPlaying()) {
                    progressBar.setVisibility(View.INVISIBLE);
                }
            }
        });
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                progressBar.setVisibility(View.INVISIBLE);
                times = player.getDuration();
                seekBar.setMax(times);
                allTimes.setText(times / 1000 + "S");
                timer = new Timer();
                task = new TimerTask() {
                    @Override
                    public void run() {
                        seekBar.setProgress(player.getCurrentPosition());
                    }
                };
                timer.schedule(task, 0, 100);
                player.setLooping(true);
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void initView() {
    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    allTimes = (TextView) findViewById(R.id.times);
    submit = (Button) findViewById(R.id.submit);
}

private void initSeekBar() {
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            player.seekTo(seekBar.getProgress());
            progressBar.setVisibility(View.VISIBLE);
        }
    });
}

class MyCallBack implements SurfaceHolder.Callback {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        player.setDisplay(holder);
    }

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

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    timer.cancel();
    task.cancel();
    flag = false;
}

class MyThread extends Thread {
    @Override
    public void run() {
        while (flag) {
            if (times == 0) {
                allTimes.post(new Runnable() {
                    @Override
                    public void run() {
                        flag = false;
                    }
                });

            } else {
                allTimes.post(new Runnable() {
                    @Override
                    public void run() {
                        allTimes.setText((times - player.getCurrentPosition()) / 1000 + "S");
                        Log.v("times", times - (player.getCurrentPosition() / 1000) + "");
                    }
                });

                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

}

布局文件

这里写图片描述

这里写图片描述

全部代码都已经在这里了,大神就不用看了,希望对初学者有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值