Android流媒体开发之-直播自定义列表

来源:互联网 发布:react native app.js 编辑:程序博客网 时间:2024/05/22 03:22

1、Android流媒体开发之-直播实现

2、Android流媒体开发之-直播自定义列表

3、Android流媒体开发之-服务器图片的加载

4、Android流媒体开发之-直播自定义分类

5、Android流媒体开发之-获取直播节目预告-1

6、Android流媒体开发之-直播界面切换电视台频道



网上大多数直播软件都会提供一个功能就是自定义,这个由于版本问题,而为了让用户看到自己想看的频道,可以让用户自己添加找到的源,然后进行播放,俗话说法不责众,这个功能是如何实现的呢,其实也是很简单的,无法就是记录下用户添加的数据,然后保存下来,这里主要用到了数据库,用户点击自己添加的源,程序会从数据库取出链接,例如http,rtsp等开头的流媒体链接,具体的实现如下,我们是在之前流媒体开发之--直播实现的基础上继续完善。

      首先就是设计一个布局,里面使用ListView来填充,custom.xml,内容如下:


  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:orientation="vertical" >  

  6.       

  7.     <TextView   

  8.         android:id="@+id/custom_title"  

  9.         android:layout_width="match_parent"  

  10.         android:layout_height="30dp"  

  11.         android:text="直播"  

  12.         android:textSize="27sp"  

  13.         android:gravity="center"  

  14.         />  

  15.   

  16.     <ListView   

  17.         android:id="@+id/custom_list"  

  18.         android:layout_width="match_parent"  

  19.         android:layout_height="wrap_content"  

  20.         ></ListView>  

  21. </LinearLayout>  


我们同时还需要一个ListView的一个Item和一个foot用来显示添加的源和实现添加功能,内容如下:

custom_list_item.xml


  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="wrap_content"  

  4.     android:layout_height="wrap_content"  

  5.     android:orientation="horizontal" >  

  6.       

  7.     <TextView   

  8.         android:id="@+id/custom_list_item_name"  

  9.         android:layout_width="100dp"  

  10.         android:layout_height="wrap_content"  

  11.         android:singleLine="true"  

  12.         android:text="频道"  

  13.         />  

  14.     <TextView   

  15.         android:id="@+id/custom_list_item_url"  

  16.         android:layout_width="match_parent"  

  17.         android:layout_height="30dp"  

  18.         android:text="视频源链接"  

  19.         />  

  20. </LinearLayout>  


custom_list_foot.xml

[html] view plain copy print?

  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:orientation="vertical" >  

  6.     <TextView   

  7.         android:layout_width="match_parent"  

  8.         android:layout_height="30dp"  

  9.         android:text="点击此处添加视频源"  

  10.         android:textSize="25sp"  

  11.         />  

  12.   

  13. </LinearLayout>  


我上面只是使用TextView来显示,如果需要美化,可以添加ImageView来实现图文并茂,布局写完后,我们就要添加一个数据库用来记录添加的视频源,为了方便起见,我们还需要定义一个自定义视频内容格式的类,内容如下:

CustomVideo.Java

[java] view plain copy print?

  1. package com.jwzhangjie.live.type;  

  2.   

  3. import java.io.Serializable;  

  4.   

  5. public class CustomVideo implements Serializable{  

  6.   

  7.     private static final long serialVersionUID = 2166585200370610099L;  

  8.   

  9.     private String name;  

  10.     private String url;  

  11.     public String getName() {  

  12.         return name;  

  13.     }  

  14.     public void setName(String name) {  

  15.         this.name = name;  

  16.     }  

  17.     public String getUrl() {  

  18.         return url;  

  19.     }  

  20.     public void setUrl(String url) {  

  21.         this.url = url;  

  22.     }  

  23. }  

 

DBHelper.java

[java] view plain copy print?

  1. package com.jwzhangjie.live.db;  

  2.   

  3. import android.content.Context;  

  4. import android.database.sqlite.SQLiteDatabase;  

  5. import android.database.sqlite.SQLiteDatabase.CursorFactory;  

  6. import android.database.sqlite.SQLiteOpenHelper;  

  7.   

  8. public class DBHelper extends SQLiteOpenHelper{  

  9.   

  10.     public static final String DB_NAME = "custom.sqlite";  

  11.     public static final String TABLE_NAME_VIDEO = "custom";  

  12.     public static final String CAMERA_SQL = "CREATE TABLE video ('_id' INTEGER PRIMARY KEY  AUTOINCREMENT, 'name' VARCHAR, 'url' VARCHAR)";  

  13.     public static int DB_VERSION = 1;  

  14.     public DBHelper(Context context, String name, CursorFactory factory,  

  15.             int version) {  

  16.         super(context, DB_NAME, null, DB_VERSION);  

  17.     }  

  18.   

  19.     @Override  

  20.     public void onCreate(SQLiteDatabase db) {  

  21.         db.execSQL(CAMERA_SQL);  

  22.     }  

  23.   

  24.     @Override  

  25.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  

  26.           

  27.     }  

  28.   

  29. }  

当然有这个还不行,我们还需要创建一个实现数据库各种操作的类,定义如下:


  1. package com.jwzhangjie.live.utils;  

  2.   

  3. import java.util.ArrayList;  

  4. import java.util.List;  

  5.   

  6. import android.content.ContentValues;  

  7. import android.content.Context;  

  8. import android.database.Cursor;  

  9. import android.database.sqlite.SQLiteDatabase;  

  10.   

  11. import com.jwzhangjie.live.db.DBHelper;  

  12. import com.jwzhangjie.live.type.CustomVideo;  

  13.   

  14. public class DBUtils {  

  15.   

  16.     private static DBHelper mDbHelper;  

  17.     public DBUtils(Context context){  

  18.         mDbHelper = new DBHelper(context);  

  19.     }  

  20.       

  21.     /** 

  22.      * insert custom video 

  23.      */  

  24.     public void insertCustomVideo(CustomVideo customVideo){  

  25.         SQLiteDatabase dbDatabase = null;  

  26.         try {  

  27.             dbDatabase = mDbHelper.getWritableDatabase();  

  28.             if (dbDatabase == null) {  

  29.                 return;  

  30.             }  

  31.             ContentValues contentValues = new ContentValues();  

  32.             contentValues.put("name", customVideo.getName());  

  33.             contentValues.put("url", customVideo.getUrl());  

  34.             dbDatabase.insertOrThrow("custom"null, contentValues);  

  35.         } catch (Exception e) {  

  36.         }finally{  

  37.             dbDatabase.close();  

  38.         }  

  39.     }  

  40.       

  41.     public void deleteOneCustomVideo(String name){  

  42.         SQLiteDatabase dbDatabase = mDbHelper.getWritableDatabase();  

  43.         try {  

  44.             String sql = "DELETE FROM custom WHERE name = "+name;  

  45.             dbDatabase.execSQL(sql);  

  46.         } catch (Exception e) {  

  47.         }finally{  

  48.             dbDatabase.close();  

  49.         }  

  50.     }  

  51.       

  52.     public List<CustomVideo> getAllCameras(){  

  53.         List<CustomVideo> list = new ArrayList<CustomVideo>();  

  54.         SQLiteDatabase dbDatabase = mDbHelper.getWritableDatabase();  

  55.         String sql = "SELECT * FROM custom";  

  56.         Cursor cursor = dbDatabase.rawQuery(sql, null);  

  57.         while (!cursor.isLast()) {  

  58.             cursor.moveToNext();  

  59.             CustomVideo video = new CustomVideo();  

  60.             video.setId(cursor.getInt(0));  

  61.             video.setName(cursor.getString(1));  

  62.             video.setUrl(cursor.getString(2));  

  63.             list.add(video);  

  64.         }  

  65.         dbDatabase.close();  

  66.         return list;  

  67.     }  

  68.       

  69.     public int getAllCustomVideoCount(){  

  70.         SQLiteDatabase db = mDbHelper.getReadableDatabase();  

  71.         Cursor cursor = db.rawQuery("select count(*) from custom"null);  

  72.         cursor.moveToNext();  

  73.         int count = cursor.getInt(0);  

  74.         cursor.close();  

  75.         db.close();  

  76.         return count;  

  77.     }  

  78.       

  79. }  


做完上述事情后,我们就要设计显示界面操作,首先就是适配器的设计:


  1. <p>package com.jwzhangjie.live.utils;</p><p>import java.util.ArrayList;  

  2. import java.util.List;</p><p>import <a href="http://lib.csdn.net/base/android" class="replace_word" title="Android知识库" target="_blank" style="color:#df3434; font-weight:bold;">Android</a>.content.ContentValues;  

  3. import android.content.Context;  

  4. import android.database.Cursor;  

  5. import android.database.sqlite.SQLiteDatabase;</p><p>import com.jwzhangjie.live.db.DBHelper;  

  6. import com.jwzhangjie.live.type.CustomVideo;</p><p>public class DBUtils {</p><p> public static DBHelper mDbHelper;  

  7.  public DBUtils(Context context){  

  8.   mDbHelper = new DBHelper(context);  

  9.  }  

  10.    

  11.  /** 

  12.   * insert custom video 

  13.   */  

  14.  public void insertCustomVideo(CustomVideo customVideo){  

  15.   SQLiteDatabase dbDatabase = null;  

  16.   try {  

  17.    dbDatabase = mDbHelper.getWritableDatabase();  

  18.    if (dbDatabase == null) {  

  19.     return;  

  20.    }  

  21.    ContentValues contentValues = new ContentValues();  

  22.    contentValues.put("name", customVideo.getName());  

  23.    contentValues.put("url", customVideo.getUrl());  

  24.    dbDatabase.insertOrThrow("custom"null, contentValues);  

  25.   } catch (Exception e) {  

  26.   }finally{  

  27.    dbDatabase.close();  

  28.   }  

  29.  }  

  30.    

  31.  public void deleteOneCustomVideo(String url){  

  32.   SQLiteDatabase dbDatabase = mDbHelper.getWritableDatabase();  

  33.   try {  

  34.    String sql = "DELETE FROM custom WHERE url = "+url;  

  35.    dbDatabase.execSQL(sql);  

  36.   } catch (Exception e) {  

  37.   }finally{  

  38.    dbDatabase.close();  

  39.   }  

  40.  }  

  41.    

  42.  public List<CustomVideo> getAllCameras(){  

  43.   List<CustomVideo> list = new ArrayList<CustomVideo>();  

  44.   SQLiteDatabase dbDatabase = mDbHelper.getWritableDatabase();  

  45.   String sql = "SELECT * FROM custom";  

  46.   Cursor cursor = dbDatabase.rawQuery(sql, null);  

  47.   while (!cursor.isLast()) {  

  48.    cursor.moveToNext();  

  49.    CustomVideo video = new CustomVideo();  

  50.    video.setId(cursor.getInt(0));  

  51.    video.setName(cursor.getString(1));  

  52.    video.setUrl(cursor.getString(2));  

  53.    list.add(video);  

  54.   }  

  55.   dbDatabase.close();  

  56.   return list;  

  57.  }  

  58.    

  59.  public int getAllCustomVideoCount(){  

  60.   SQLiteDatabase db = mDbHelper.getReadableDatabase();  

  61.   Cursor cursor = db.rawQuery("select count(*) from custom"null);  

  62.   cursor.moveToNext();  

  63.   int count = cursor.getInt(0);  

  64.   cursor.close();  

  65.   db.close();  

  66.   return count;  

  67.  }  

  68.    

  69. }  

  70. </p>  

适配器设置完毕后,我们剩下显示界面了

  1. package com.jwzhangjie.live;  

  2.   

  3. import java.util.ArrayList;  

  4. import java.util.List;  

  5.   

  6. import com.jwzhangjie.live.adapter.CustomVideoAdapter;  

  7. import com.jwzhangjie.live.type.CustomVideo;  

  8. import com.jwzhangjie.live.utils.DBUtils;  

  9.   

  10. import android.app.Activity;  

  11. import android.content.Intent;  

  12. import android.os.Bundle;  

  13. import android.view.View;  

  14. import android.view.View.OnClickListener;  

  15. import android.widget.AdapterView;  

  16. import android.widget.AdapterView.OnItemClickListener;  

  17. import android.widget.Button;  

  18. import android.widget.EditText;  

  19. import android.widget.ListView;  

  20.   

  21. public class CustomVideoList extends Activity implements OnItemClickListener, OnClickListener{  

  22.   

  23.     public ListView customVideoListView;  

  24.     public CustomVideoAdapter listAdapter;  

  25.     private List<CustomVideo> listVideos;  

  26.     public Button addBtn;  

  27.     public EditText addUrl;  

  28.     DBUtils dbUtils = new DBUtils(this);  

  29.     @Override  

  30.     protected void onCreate(Bundle savedInstanceState) {  

  31.         super.onCreate(savedInstanceState);  

  32.         setContentView(R.layout.custom);  

  33.         addBtn = (Button)findViewById(R.id.add);  

  34.         addUrl = (EditText)findViewById(R.id.addurl);  

  35.         customVideoListView = (ListView)findViewById(R.id.custom_list);  

  36.         listAdapter = new CustomVideoAdapter(this);  

  37.         customVideoListView.setAdapter(listAdapter);  

  38.         customVideoListView.setOnItemClickListener(this);  

  39.         addBtn.setOnClickListener(this);  

  40.         addUrl.setText("http://74.82.62.53:1935/liverepeater/10.stream/playlist.m3u8");  

  41.         int size = dbUtils.getAllCustomVideoCount();  

  42.         if (size != 0) {  

  43.             listVideos = dbUtils.getAllCameras();  

  44.             listAdapter.setCustomVideoList(listVideos);  

  45.         }else {  

  46.             listVideos = new ArrayList<CustomVideo>();  

  47.         }  

  48.     }  

  49.     @Override  

  50.     protected void onResume() {  

  51.         super.onResume();  

  52.     }  

  53.     @Override  

  54.     public void onClick(View v) {  

  55.         if (v == addBtn) {  

  56.             if (addUrl.getText() != null) {  

  57.                 CustomVideo customVideo = new CustomVideo();  

  58.                 customVideo.setName("视频");  

  59.                 customVideo.setUrl(addUrl.getText().toString());  

  60.                 dbUtils.insertCustomVideo(customVideo);  

  61.                 listAdapter.insertCustomVideo(customVideo);  

  62.                 listVideos.add(customVideo);  

  63.             }  

  64.         }  

  65.     }  

  66.     @Override  

  67.     public void onItemClick(AdapterView<?> parent, View view, int position,  

  68.             long id) {  

  69.         Intent intent = new Intent();  

  70.         intent.setClass(CustomVideoList.this, JieVideoPlayer.class);  

  71.         intent.putExtra("path", listVideos.get(position).getUrl());  

  72.         startActivity(intent);  

  73.     }  

  74.       

  75.   

  76. }  



效果如下:

 

 

点击你选择的视频也能够播放视频,自定义功能就这些。


0 0
原创粉丝点击