流媒體開發之-直播自定義分類


在開發直播的時候,源涉及到版權問題,所以軟件通常會提供自定義功能,讓用戶自己添加,這樣就與軟件開發者沒有關系了,當然如果用戶添加很多自定義節目,會讓用戶看起來很亂,這會影響用戶的使用心情,所以我們可以顯示更QQ一樣的分類存儲用戶的自定義的源,先看一下效果:







首先先定義布局界面和Item布局

customexpandablelistview.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/expandParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>

</LinearLayout>

custom_expanditem.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/expand_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:textSize="25sp"        >    </TextView></LinearLayout>

布局寫好了后,我們在編寫一個適配器
CustomExpandableAdapter.java

package com.example.list;import java.util.List;import com.example.test.R;import android.app.Activity;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;public class CustomExpandableAdapter extends BaseExpandableListAdapter{private List<String> groupArray;private List<List<String[]>> childArray;private LayoutInflater mLayoutInflater;Activity activity;public CustomExpandableAdapter(Activity activity, List<String> groupArray, List<List<String[]>> childArray){this.activity = activity;this.groupArray = groupArray;this.childArray = childArray;mLayoutInflater = LayoutInflater.from(activity);}@Overridepublic int getGroupCount() {if (groupArray != null) {return groupArray.size();}else {return 0;}}@Overridepublic int getChildrenCount(int groupPosition) {return childArray.get(groupPosition).size();}@Overridepublic Object getGroup(int groupPosition) {return groupArray.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {return childArray.get(groupPosition).get(childPosition)[0];}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView == null) {convertView = mLayoutInflater.inflate(R.layout.custom_expanditem, null);viewHolder = new ViewHolder();viewHolder.name = (TextView)convertView.findViewById(R.id.expand_name);viewHolder.name.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);viewHolder.name.setPadding(60 , 0 , 0 , 0 );convertView.setTag(viewHolder);}else {viewHolder = (ViewHolder)convertView.getTag();}viewHolder.name.setText(groupArray.get(groupPosition));        return  convertView; }@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView == null) {convertView = mLayoutInflater.inflate(R.layout.custom_expanditem, null);viewHolder = new ViewHolder();viewHolder.name = (TextView)convertView.findViewById(R.id.expand_name);viewHolder.name.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);viewHolder.name.setPadding(60 , 0 , 0 , 0 );convertView.setTag(viewHolder);}else {viewHolder = (ViewHolder)convertView.getTag();}viewHolder.name.setText(childArray.get(groupPosition).get(childPosition)[0]);        return  convertView; }class ViewHolder{TextView name;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}
最后就是主界面功能了

package com.example.list;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import com.example.test.R;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.widget.ExpandableListView;import android.widget.Toast;public class CustomExpand extends Activity{private List<String> groupArray;private List<List<String[]>> childArray;CustomExpandableAdapter adapter;ExpandableListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.customexpandablelistview);groupArray = new ArrayList<String>();childArray = new ArrayList<List<String[]>>();ReadSelfChannel("tv.txt");listView = (ExpandableListView) findViewById(R.id.expandableListView);adapter = new CustomExpandableAdapter(this, groupArray, childArray);listView.setAdapter(adapter);}public void ReadSelfChannel(String filename){String path = null;File file = null;boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);if (sdCardExist) {path = Environment.getExternalStorageDirectory()+"/"+filename;file = new File(path);if (!file.exists()) {Toast.makeText(this, "請檢查文件是否存在", Toast.LENGTH_LONG).show();return;}}else {Toast.makeText(this, "請檢查SD卡是否存在", Toast.LENGTH_LONG).show();return;}try {InputStream inStream = new FileInputStream(file);if (inStream != null) {InputStreamReader inputreader = new InputStreamReader(inStream); BufferedReader buffreader = new BufferedReader(inputreader); String line; String[] splitArray; int lastGroup = 0; while (( line = buffreader.readLine()) != null) { splitArray = line.split(","); if (splitArray != null && splitArray.length == 3) { lastGroup = groupArray.size() - 1; if (lastGroup == -1) { lastGroup = 0; groupArray.add(splitArray[0]);}else if (groupArray.get(lastGroup).equals(splitArray[0])) {//首先檢測最后一個類型}else {lastGroup = CheckGroupName(splitArray[0]);if (lastGroup == -1) {groupArray.add(splitArray[0]);lastGroup = groupArray.size() - 1;}} String[] tempArray = new String[2]; tempArray[0] = splitArray[1]; tempArray[1] = splitArray[2]; List<String[]> temlist; int size = childArray.size(); if (size == 0 || (size -1) < lastGroup) { temlist = new ArrayList<String[]>();}else { temlist = childArray.get(lastGroup); childArray.remove(lastGroup);} temlist.add(tempArray); childArray.add(lastGroup, temlist);}} buffreader.close();}} catch (Exception e) {e.printStackTrace();}}public int CheckGroupName(String name){int count = groupArray.size();for (int i = 0; i < count; i++) {if (name.equals(groupArray.get(i))) {return i;}}return -1;}}

源的格式如下:
央視,CCTV綜合,http://live.gslb.letv.com/gslb?stream_id=cctv1&tag=live&ext=m3u8&sign=live_tv央視,CCTV經濟,http://live.gslb.letv.com/gslb?stream_id=cctv2&tag=live&ext=m3u8&sign=live_tv  央視,CCTV文藝,http://live.gslb.letv.com/gslb?stream_id=cctv3&tag=live&ext=m3u8&sign=live_tv 央視,CCTV國際,http://live.gslb.letv.com/gslb?stream_id=cctv4&tag=live&ext=m3u8&sign=live_tv央視,CCTV體育,http://live.gslb.letv.com/gslb?stream_id=cctv5_800&tag=live&ext=m3u8&sign=live_tv 央視,CCTV電影,http://live.gslb.letv.com/gslb?stream_id=cctv6&tag=live&ext=m3u8&sign=live_tv                央視,CCTV農業軍事,http://live.gslb.letv.com/gslb?stream_id=cctv7&tag=live&ext=m3u8&sign=live_tv 衛視,河南衛視,http://live.gslb.letv.com/gslb?stream_id=henan&tag=live&ext=m3u8&sign=live_tv衛視,東南衛視,http://live.gslb.letv.com/gslb?stream_id=dongnan&tag=live&ext=m3u8&sign=live_tv衛視,東方衛視,http://live.gslb.letv.com/gslb?stream_id=dongfang&tag=live&ext=m3u8&sign=live_tv衛視,北京衛視,http://live.gslb.letv.com/gslb?stream_id=bjws&tag=live&ext=m3u8&sign=live_tv            衛視,江蘇衛視,http://live.gslb.letv.com/gslb?stream_id=jiangsu&tag=live&ext=m3u8&sign=live_tv       衛視,遼寧衛視,http://live.gslb.letv.com/gslb?stream_id=liaoning&tag=live&ext=m3u8&sign=live_tv      衛視,陝西衛視,http://live.gslb.letv.com/gslb?stream_id=shanxi1&tag=live&ext=m3u8&sign=live_tv衛視,江西衛視,http://live.gslb.letv.com/gslb?stream_id=jiangxi&tag=live&ext=m3u8&sign=live_tv衛視,甘肅衛視,http://live.gslb.letv.com/gslb?stream_id=gansu&tag=live&ext=m3u8&sign=live_tv          衛視,河北衛視,http://live.gslb.letv.com/gslb?stream_id=hebei&tag=live&ext=m3u8&sign=live_tv       樂視,樂視電影,http://live.gslb.letv.com/gslb?stream_id=letv_movie_800&tag=live&ext=m3u8&sign=live_tv樂視,樂視電視劇,http://live.gslb.letv.com/gslb?stream_id=letv_tv_800&tag=live&ext=m3u8&sign=live_tv樂視,樂視動漫,http://live.gslb.letv.com/gslb?stream_id=letv_comic_800&tag=live&ext=m3u8&sign=live_tv樂視,樂視體育,http://live.gslb.letv.com/gslb?stream_id=letv_sports_800&tag=live&ext=m3u8&sign=live_tv樂視,樂視1080p,http://live.gslb.letv.com/gslb?stream_id=letv_1080P_3000&tag=live&ext=m3u8&sign=live_tv樂視,樂視文娛,http://live.gslb.letv.com/gslb?stream_id=letv_ent_800&tag=live&ext=m3u8&sign=live_tv樂視,樂視音樂,http://live.gslb.letv.com/gslb?stream_id=letv_music_800&tag=live&ext=m3u8&sign=live_tv    其他,翡翠音樂台,rtsp://69.197.154.238/tvbm.kmj其他,高清舞樂台,rtsp://69.197.154.238/dance其他,台灣民族樂,rtsp://69.197.154.238/twmusic其他,星音樂頻道,rtsp://69.197.154.238/stars其他,DVMC經典樂,rtsp://69.197.154.238/dvmc其他,歐美性感樂,rtsp://69.197.154.238/6mtv.sexy.其他,好萊塢大片,rtsp://69.197.154.238/hlw.ueixeyfgecvyh其他,熱門電影台,rtsp://69.197.154.238/max.awixeyfgecvydr

這樣就完成了,非常方便。

关注微信公众号

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com