嗯,那个软件活在青春里,记录一下吧。
项目结构
首先,因为是网络视频播放器,那么我必须要有相应的权限,第一部去manifest注册网络权限。
main_activity代码:
package com.glsite.qvod;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private SeekBar mSeekBar1;
private SurfaceView mSv;
private ProgressBar mPb;
private MediaPlayer mMediaPlayer;
private Timer mTimer;
private TimerTask mTask;
private SharedPreferences mSp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
initEvent();
}
/**
* 初始化事件
*/
private void initEvent() {
//写mseekbar的改变监听
mSeekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if(mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.seekTo(mSeekBar1.getProgress());
}
}
});
mSv.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
System.out.println("surcfaceview被创建了");
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource("http://10.0.204.124:8080/Day10/2.mp4");
//播放视频必须设置播放的容器,通过msv得到他的holder
mMediaPlayer.setDisplay(mSv.getHolder());
mMediaPlayer.prepareAsync();//异步的准备,开启子线程
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
SharedPreferences.Editor editor = mSp.edit();
editor.putInt("position",0);
editor.commit();
}
});
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
mPb.setVisibility(View.INVISIBLE);
//mSeekBar1.setVisibility(View.INVISIBLE);
int position = mSp.getInt("position", 0);
mMediaPlayer.seekTo(position);
mMediaPlayer.start();
mTimer = new Timer();
mTask = new TimerTask() {
@Override
public void run() {
int max = mMediaPlayer.getDuration();
int progress = mMediaPlayer.getCurrentPosition();
mSeekBar1.setMax(max);
mSeekBar1.setProgress(progress);
}
};
mTimer.schedule(mTask,0,500);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
System.out.println("suraceview被销毁了");
if(mMediaPlayer != null && mMediaPlayer.isPlaying()) {
int position = mMediaPlayer.getCurrentPosition();
SharedPreferences.Editor editor = mSp.edit();
editor.putInt("position",position);
editor.commit();
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
mTimer.cancel();
mTask.cancel();
mTimer = null;
mTask = null;
}
}
});
}
/**
* 初始化数据
*/
private void initData() {
mSp = getSharedPreferences("config", MODE_WORLD_READABLE);
}
/**
* 初始化界面
*/
private void initView() {
setContentView(R.layout.activity_main);
mSeekBar1 = findViewById(R.id.seekBar1);
mPb = findViewById(R.id.pb);
mSv = findViewById(R.id.sv);
}
//写一个点击事件
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
System.out.println("-----------------");
mSeekBar1.setVisibility(View.VISIBLE);
new Thread(){
@Override
public void run() {
SystemClock.sleep(3000);
runOnUiThread(new Runnable() {
@Override
public void run() {
mSeekBar1.setVisibility(View.INVISIBLE);
}
});
}
}.start();
}
return super.onTouchEvent(event);
}
}
布局文件代码:
<?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/sv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
好了,小快播就好了,嘻嘻。