我已经使用RecyclerView来显示我解析的JSON数据列表。它包含视频网址。现在我想在该列表中播放这些视频。所以我的第一个问题是什么是最好的方法(即ExoPlayer) ,MediaPlayer,SurfaceView,TextureView或其他)和第二个问题是如何通过recyclerview适配器实现它。请帮助我。
上下文代码
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Http Connection";
private List<Live> liveList = new ArrayList<>();
private RecyclerView recyclerView;
private LiveAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.listView);
final String url = "My URL";
new AsyncHttpTask().execute(url);
}
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
@Override
protected Integer doInBackground(String... params) {
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
Integer result = 0;
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode==200){
inputStream = new BufferedInputStream(urlConnection.getInputStream());
String response = convertInputStreamToString(inputStream);
parseResult(response);
result = 1;
}else{
result = 0;
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
if(result == 1){
mAdapter = new LiveAdapter(getApplicationContext(),liveList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}else{
Log.e(TAG, "Failed to fetch data!");
}
}
}
private String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null){
result += line;
}
/* Close Stream */
if(null!=inputStream){
inputStream.close();
}
return result;
}
private void parseResult(String result) {
try{
JSONObject response = new JSONObject(result);
String code = response.getString("code");
JSONObject results = response.optJSONObject("result");
JSONArray live = results.optJSONArray("live");
for(int i=0; i< live.length();i++ ){
JSONObject lives = live.optJSONObject(i);
Live l=new Live();
l.id = lives.getString("id");
l.title = lives.getString("title");
l.category_id = lives.getString("category_id");
l.category = lives.getString("category");
l.url = lives.getString("url");
l.thumbnail_small=lives.getString("thumbnail_small");
l.thumbnail_large=lives.getString("thumbnail_large");
l.thumbnail_banner=lives.getString("thumbnail_banner");
l.type=lives.getString("type");
l.likes=lives.getString("likes");
l.watch=lives.getString("watch");
liveList.add(i,l);
}
}catch (JSONException e){
e.printStackTrace();
}
}
}
LiveAdapter.java
public class LiveAdapter extends RecyclerView.Adapter<LiveAdapter.MyViewHolder> {
private static String TAG = "LiveAdapter";
private List<Live> liveList;
private Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, ids, category_id,category,type,like,watch;
public ImageView ts,tl,tb;
public VideoView url;
public MyViewHolder(View view) {
super(view);
ids = (TextView) view.findViewById(R.id.id);
category_id = (TextView) view.findViewById(R.id.category_id);
category = (TextView) view.findViewById(R.id.category);
url = (VideoView) view.findViewById(R.id.url);
ts = (ImageView) view.findViewById(R.id.ts);
tl = (ImageView) view.findViewById(R.id.tl);
tb = (ImageView) view.findViewById(R.id.tb);
type = (TextView) view.findViewById(R.id.type);
like = (TextView) view.findViewById(R.id.like);
watch = (TextView) view.findViewById(R.id.watch);
title = (TextView) view.findViewById(R.id.title);
}
}
public LiveAdapter(Context context, List<Live> liveList) {
this.liveList = liveList;
this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Live live = liveList.get(position);
holder.title.setText(live.getTitle());
holder.ids.setText(live.getId());
holder.category_id.setText(live.getCategory_id());
holder.category.setText(live.getCategory());
holder.type.setText(live.getType());
holder.like.setText(live.getLikes());
holder.watch.setText(live.getWatch());
Picasso.with(context).load(liveList.get(position).getThumbnail_small()).resize(120, 60).into(holder.ts);
Picasso.with(context).load(liveList.get(position).getThumbnail_large()).resize(240, 120).into(holder.tl);
Picasso.with(context).load(liveList.get(position).getThumbnail_banner()).resize(120, 60).into(holder.tb);
String video_url=liveList.get(position).getUrl();
holder.url.setVideoURI(Uri.parse(video_url));
}
@Override
public int getItemCount() {
return liveList.size();
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/listView"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<!-- Name Label -->
<TextView
android:id="@+id/id"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:text="1"
android:textColor="#23e1b5"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/title"
android:layout_width="250dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/category_id"
android:text="ZOOM"
android:paddingTop="6dip"
android:paddingBottom="2dip"
android:textColor="#23e1b5"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/category_id"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/id"
android:text="10"
android:paddingTop="6dip"
android:textColor="#23e1b5"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/category"
android:layout_width="100dp"
android:layout_below="@+id/id"
android:layout_marginTop="10dp"
android:text="MOVIES"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#942e8f"
android:textSize="16sp"
android:textStyle="bold" />
<VideoView
android:id="@+id/url"
android:layout_below="@+id/category"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:paddingBottom="2dip"
android:paddingTop="6dip"
/>
<ImageView
android:id="@+id/ts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/url"
android:paddingBottom="2dip"
android:paddingTop="6dip"
/>
<ImageView
android:id="@+id/tl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:layout_below="@+id/ts"
android:paddingTop="6dip"
/>
<ImageView
android:id="@+id/tb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tl"
android:paddingBottom="2dip"
android:paddingTop="6dip" />
<TextView
android:id="@+id/type"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tb"
android:paddingBottom="2dip"
android:text="Live"
android:paddingTop="6dip"
android:textColor="#201b20"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/like"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/type"
android:layout_marginLeft="10dp"
android:layout_below="@+id/tb"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:text="0"
android:textColor="#23e1b5"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/rating"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tb"
android:layout_toRightOf="@+id/like"
android:layout_marginLeft="10dp"
android:text="0"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#942e8f"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/watch"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tb"
android:layout_toRightOf="@+id/rating"
android:layout_marginLeft="10dp"
android:text="0"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#23e1b5"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>