流媒体开发之--直播实现
可可工作室出品:
欢迎使用可可直播视频播放器:
http://pan.baidu.com/share/link?shareid=3291150676&uk=4111208708
网上大多数直播软件都会提供一个功能就是自定义,这个由于版本问题,而为了让用户看到自己想看的频道,可以让用户自己添加找到的源,然后进行播放,俗话说法不责众,这个功能是如何实现的呢,其实也是很简单的,无法就是记录下用户添加的数据,然后保存下来,这里主要用到了数据库,用户点击自己添加的源,程序会从数据库取出链接,例如http,rtsp等开头的流媒体链接,具体的实现如下,我们是在之前流媒体开发之--直播实现的基础上继续完善。
首先就是设计一个布局,里面使用ListView来填充,custom.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/custom_title" android:layout_width="match_parent" android:layout_height="30dp" android:text="直播" android:textSize="27sp" android:gravity="center" /> <ListView android:id="@+id/custom_list" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
我们同时还需要一个ListView的一个Item和一个foot用来显示添加的源和实现添加功能,内容如下:
custom_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/custom_list_item_name" android:layout_width="100dp" android:layout_height="wrap_content" android:singleLine="true" android:text="频道" /> <TextView android:id="@+id/custom_list_item_url" android:layout_width="match_parent" android:layout_height="30dp" android:text="视频源链接" /> </LinearLayout>
custom_list_foot.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:layout_width="match_parent" android:layout_height="30dp" android:text="点击此处添加视频源" android:textSize="25sp" /> </LinearLayout>
我上面只是使用TextView来显示,如果需要美化,可以添加ImageView来实现图文并茂,布局写完后,我们就要添加一个数据库用来记录添加的视频源,为了方便起见,我们还需要定义一个自定义视频内容格式的类,内容如下:
CustomVideo.java
package com.jwzhangjie.live.type; import java.io.Serializable; public class CustomVideo implements Serializable{ private static final long serialVersionUID = 2166585200370610099L; private String name; private String url; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
DBHelper.java
package com.jwzhangjie.live.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper{ public static final String DB_NAME = "custom.sqlite"; public static final String TABLE_NAME_VIDEO = "custom"; public static final String CAMERA_SQL = "CREATE TABLE video ('_id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' VARCHAR, 'url' VARCHAR)"; public static int DB_VERSION = 1; public DBHelper(Context context, String name, CursorFactory factory, int version) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CAMERA_SQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
当然有这个还不行,我们还需要创建一个实现数据库各种操作的类,定义如下:
package com.jwzhangjie.live.utils; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.jwzhangjie.live.db.DBHelper; import com.jwzhangjie.live.type.CustomVideo; public class DBUtils { private static DBHelper mDbHelper; public DBUtils(Context context){ mDbHelper = new DBHelper(context); } /** * insert custom video */ public void insertCustomVideo(CustomVideo customVideo){ SQLiteDatabase dbDatabase = null; try { dbDatabase = mDbHelper.getWritableDatabase(); if (dbDatabase == null) { return; } ContentValues contentValues = new ContentValues(); contentValues.put("name", customVideo.getName()); contentValues.put("url", customVideo.getUrl()); dbDatabase.insertOrThrow("custom", null, contentValues); } catch (Exception e) { }finally{ dbDatabase.close(); } } public void deleteOneCustomVideo(String name){ SQLiteDatabase dbDatabase = mDbHelper.getWritableDatabase(); try { String sql = "DELETE FROM custom WHERE name = "+name; dbDatabase.execSQL(sql); } catch (Exception e) { }finally{ dbDatabase.close(); } } public List<CustomVideo> getAllCameras(){ List<CustomVideo> list = new ArrayList<CustomVideo>(); SQLiteDatabase dbDatabase = mDbHelper.getWritableDatabase(); String sql = "SELECT * FROM custom"; Cursor cursor = dbDatabase.rawQuery(sql, null); while (!cursor.isLast()) { cursor.moveToNext(); CustomVideo video = new CustomVideo(); video.setId(cursor.getInt(0)); video.setName(cursor.getString(1)); video.setUrl(cursor.getString(2)); list.add(video); } dbDatabase.close(); return list; } public int getAllCustomVideoCount(){ SQLiteDatabase db = mDbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select count(*) from custom", null); cursor.moveToNext(); int count = cursor.getInt(0); cursor.close(); db.close(); return count; } }
做完上述事情后,我们就要设计显示界面操作,首先就是适配器的设计:
package com.jwzhangjie.live.utils;
import java.util.ArrayList; import java.util.List;
import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;
import com.jwzhangjie.live.db.DBHelper; import com.jwzhangjie.live.type.CustomVideo;
public class DBUtils {
public static DBHelper mDbHelper; public DBUtils(Context context){ mDbHelper = new DBHelper(context); } /** * insert custom video */ public void insertCustomVideo(CustomVideo customVideo){ SQLiteDatabase dbDatabase = null; try { dbDatabase = mDbHelper.getWritableDatabase(); if (dbDatabase == null) { return; } ContentValues contentValues = new ContentValues(); contentValues.put("name", customVideo.getName()); contentValues.put("url", customVideo.getUrl()); dbDatabase.insertOrThrow("custom", null, contentValues); } catch (Exception e) { }finally{ dbDatabase.close(); } } public void deleteOneCustomVideo(String url){ SQLiteDatabase dbDatabase = mDbHelper.getWritableDatabase(); try { String sql = "DELETE FROM custom WHERE url = "+url; dbDatabase.execSQL(sql); } catch (Exception e) { }finally{ dbDatabase.close(); } } public List<CustomVideo> getAllCameras(){ List<CustomVideo> list = new ArrayList<CustomVideo>(); SQLiteDatabase dbDatabase = mDbHelper.getWritableDatabase(); String sql = "SELECT * FROM custom"; Cursor cursor = dbDatabase.rawQuery(sql, null); while (!cursor.isLast()) { cursor.moveToNext(); CustomVideo video = new CustomVideo(); video.setId(cursor.getInt(0)); video.setName(cursor.getString(1)); video.setUrl(cursor.getString(2)); list.add(video); } dbDatabase.close(); return list; } public int getAllCustomVideoCount(){ SQLiteDatabase db = mDbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select count(*) from custom", null); cursor.moveToNext(); int count = cursor.getInt(0); cursor.close(); db.close(); return count; } }
适配器设置完毕后,我们剩下显示界面了
package com.jwzhangjie.live; import java.util.ArrayList; import java.util.List; import com.jwzhangjie.live.adapter.CustomVideoAdapter; import com.jwzhangjie.live.type.CustomVideo; import com.jwzhangjie.live.utils.DBUtils; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; public class CustomVideoList extends Activity implements OnItemClickListener, OnClickListener{ public ListView customVideoListView; public CustomVideoAdapter listAdapter; private List<CustomVideo> listVideos; public Button addBtn; public EditText addUrl; DBUtils dbUtils = new DBUtils(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom); addBtn = (Button)findViewById(R.id.add); addUrl = (EditText)findViewById(R.id.addurl); customVideoListView = (ListView)findViewById(R.id.custom_list); listAdapter = new CustomVideoAdapter(this); customVideoListView.setAdapter(listAdapter); customVideoListView.setOnItemClickListener(this); addBtn.setOnClickListener(this); addUrl.setText("http://74.82.62.53:1935/liverepeater/10.stream/playlist.m3u8"); int size = dbUtils.getAllCustomVideoCount(); if (size != 0) { listVideos = dbUtils.getAllCameras(); listAdapter.setCustomVideoList(listVideos); }else { listVideos = new ArrayList<CustomVideo>(); } } @Override protected void onResume() { super.onResume(); } @Override public void onClick(View v) { if (v == addBtn) { if (addUrl.getText() != null) { CustomVideo customVideo = new CustomVideo(); customVideo.setName("视频"); customVideo.setUrl(addUrl.getText().toString()); dbUtils.insertCustomVideo(customVideo); listAdapter.insertCustomVideo(customVideo); listVideos.add(customVideo); } } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(); intent.setClass(CustomVideoList.this, JieVideoPlayer.class); intent.putExtra("path", listVideos.get(position).getUrl()); startActivity(intent); } }
效果如下:
点击你选择的视频也能够播放视频,自定义功能就这些。
欢迎使用可可直播视频播放器:
http://pan.baidu.com/share/link?shareid=3291150676&uk=4111208708