一、SurfaceView的用法:
1、在布局文件中设计相关布局;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.qf.day_01.Main2Activity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/sv"/>
</RelativeLayout>
2、在Java代码中实现业务代码
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class Main2Activity extends AppCompatActivity implements SurfaceHolder.Callback {
private SurfaceView mSurfaceView;
private SurfaceHolder mHolder;
private MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//查找相关控件
mSurfaceView = (SurfaceView) findViewById(R.id.sv);
mHolder = mSurfaceView.getHolder();//得到SurfaceView控制器
mHolder.addCallback(Main2Activity.this);//添加回调方法
}
private void initMediaPlayer() {
//新建一个媒体播放器
mPlayer = new MediaPlayer();
Uri mUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.mtv);
try {
//设置媒体资源
mPlayer.setDataSource(this,mUri);
mPlayer.setDisplay(mHolder);//设置一个SurfaceView的控制器
mPlayer.prepareAsync();//开始准备
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mPlayer.start();//准备好之后开始播放
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
initMediaPlayer();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
二、仿腾讯新闻中页面播放视频
1、在布局文件中设计布局(简化)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.qf.day_01.Main4Activity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv">
</ListView>
</RelativeLayout>
2、在Java代码中找到控件并设置适配器
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class Main4Activity extends AppCompatActivity {
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
mListView = (ListView) findViewById(R.id.lv);
mListView.setAdapter(new MyAdapter(this));
}
}
3、设计ListView的item布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sv_player"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc"
android:id="@+id/img"
android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@android:drawable/ic_media_play"/>
</RelativeLayout>
4、自定义ListView的适配器
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.io.IOException;
/**
* 只需要一个MediaPlayer就可以了,因为只有一个视频在播放
*/
public class MyAdapter extends BaseAdapter implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
private final MediaPlayer mPlayer;
private Context mContext;
//当前应该播放视频的位置
private int currentPosition = -1;
public MyAdapter(Context context) {
mContext = context;
mPlayer = new MediaPlayer();
//设置监听,必须的监听。
mPlayer.setOnPreparedListener(this);
//设置监听。当视频播放完成时候的监听。
mPlayer.setOnCompletionListener(this);
}
@Override
public int getCount() {
return 10;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
/**
* 因为getView方法,在页面滑动的时候(页面改变)都会调用,
* 所以,判断当前是否是应该播放视频的位置。
* 如果不是,停止播放。
* 如果是,播放。
* @param position
* @param convertView
* @param parent
* @return
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder=null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
mViewHolder = new ViewHolder();
mViewHolder.sv = (SurfaceView) convertView.findViewById(R.id.sv_player);
mViewHolder.img = (ImageView) convertView.findViewById(R.id.img);
//在这个里面设置ImageView的点击监听。
mViewHolder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取到tag,就是当前的位置
int position = (int) v.getTag();
//赋值给成员变量
currentPosition = position;
//通知适配器更新
notifyDataSetChanged();
}
});
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
Object mTag = mViewHolder.sv.getTag();
if (mTag != null) {
//如果有数据
int oldPosition= (int) mTag;
//如果SurfaceView的位置和当前播放的位置不一样
if (oldPosition==currentPosition && oldPosition!=position) {
if (mPlayer.isPlaying()) {
//如果正在播放,而且位置又和应该播放的位置不一样。就停止
mPlayer.stop();
currentPosition = -1;
}
}
}
//设置Tag
mViewHolder.sv.setTag(position);
mViewHolder.img.setTag(position);
//判断当前创建视图的位置,是不是应该播放视频的位置
if (currentPosition == position) {
//如果是
mViewHolder.img.setVisibility(View.INVISIBLE);
mViewHolder.sv.setVisibility(View.VISIBLE);
Uri mParse = Uri.parse("android.resource://" + mContext.getPackageName() + "/" + R.raw.mtv);
//重置
mPlayer.reset();
try {
//设置数据源
mPlayer.setDataSource(mContext, mParse);
//给MediaPlayer设置一个SurfaceHolder
mPlayer.setDisplay(mViewHolder.sv.getHolder());
//开始准备
mPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
} else {
mViewHolder.img.setVisibility(View.VISIBLE);
mViewHolder.sv.setVisibility(View.INVISIBLE);
}
return convertView;
}
@Override
public void onPrepared(MediaPlayer mp) {
//当准备好之后
mp.start();
}
@Override
public void onCompletion(MediaPlayer mp) {
//只需要对currentPosition设置为-1就可以了
currentPosition = -1;
}
private static class ViewHolder{
SurfaceView sv;
ImageView img;
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章