当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 使用Vitamio打造自己的万能播放器(9)—— 在线播放 (在线电视)

Android 使用Vitamio打造自己的万能播放器(9)—— 在线播放 (在线电视)

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

 如果不想自己去找视频看,以传统方式看电视也不错,比如cctv、湖南卫视等。本章从网络收集几百个电视台的地址,采用多级分类方式呈现,极大丰富在线播放部分的内容。

系列

1、android 使用vitamio打造自己的万能播放器(1)——准备  

2、android 使用vitamio打造自己的万能播放器(2)—— 手势控制亮度、音量、缩放 

3、android 使用vitamio打造自己的万能播放器(3)——本地播放(主界面、视频列表) 

4、android 使用vitamio打造自己的万能播放器(4)——本地播放(快捷搜索、数据存储)

5、android 使用vitamio打造自己的万能播放器(5)——在线播放(播放优酷视频)

6、android 使用vitamio打造自己的万能播放器(6)——在线播放(播放列表)

7、android 使用vitamio打造自己的万能播放器(7)——在线播放(下载视频)

8、android 使用vitamio打造自己的万能播放器(8)——细节优化

正文

 一、目标

  以多级目录分类方式在在线视频栏目下添加电视台。

 

 

 二、主要代码

  电视台的地址目前是存在xml文件里,那么本文代码主要就是解析xml的数据了。

package com.nmbb.oplayer.ui.helper;

import java.io.ioexception;
import java.util.arraylist;

import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.parsers.parserconfigurationexception;

import org.w3c.dom.document;
import org.w3c.dom.element;
import org.w3c.dom.namednodemap;
import org.w3c.dom.node;
import org.w3c.dom.nodelist;
import org.xml.sax.saxexception;

import android.content.context;

import com.nmbb.oplayer.po.onlinevideo;

/** 从xml读取电视台节目 */
public class xmlreaderhelper {

  /** 获取所有电视分类 */
  public static arraylist<onlinevideo> getallcategory(final context context) {
    arraylist<onlinevideo> result = new arraylist<onlinevideo>();
    documentbuilderfactory docbuilderfactory = null;
    documentbuilder docbuilder = null;
    document doc = null;
    try {
      docbuilderfactory = documentbuilderfactory.newinstance();
      docbuilder = docbuilderfactory.newdocumentbuilder();
      // xml file 放到 assets目录中的
      doc = docbuilder.parse(context.getresources().getassets()
          .open("online.xml"));
      // root element
      element root = doc.getdocumentelement();
      nodelist nodelist = root.getelementsbytagname("category");
      for (int i = 0; i < nodelist.getlength(); i++) {
        node node = nodelist.item(i);// category
        onlinevideo ov = new onlinevideo();
        namednodemap attr = node.getattributes();
        ov.title = attr.getnameditem("name").getnodevalue();
        ov.id = attr.getnameditem("id").getnodevalue();
        ov.category = 1;
        ov.level = 2;
        ov.is_category = true;
        result.add(ov);
        // read node
      }
    } catch (ioexception e) {
    } catch (saxexception e) {
    } catch (parserconfigurationexception e) {
    } finally {
      doc = null;
      docbuilder = null;
      docbuilderfactory = null;
    }
    return result;
  }

  /** 读取分类下所有电视地址 */
  public static arraylist<onlinevideo> getvideos(final context context,
      string categoryid) {
    arraylist<onlinevideo> result = new arraylist<onlinevideo>();
    documentbuilderfactory docbuilderfactory = null;
    documentbuilder docbuilder = null;
    document doc = null;
    try {
      docbuilderfactory = documentbuilderfactory.newinstance();
      docbuilder = docbuilderfactory.newdocumentbuilder();
      // xml file 放到 assets目录中的
      doc = docbuilder.parse(context.getresources().getassets()
          .open("online.xml"));
      // root element
      element root = doc.getelementbyid(categoryid);
      if (root != null) {
        nodelist nodelist = root.getchildnodes();
        for (int i = 0, j = nodelist.getlength(); i < j; i++) {
          node basenode = nodelist.item(i);

          if (!"item".equals(basenode.getnodename()))
            continue;
          string id = basenode.getfirstchild().getnodevalue();
          if (id == null)
            continue;
          onlinevideo ov = new onlinevideo();
          ov.id = id;

          element el = doc.getelementbyid(ov.id);
          if (el != null) {
            ov.title = el.getattribute("title");
            ov.icon_url = el.getattribute("image");
            ov.level = 3;
            ov.category = 1;
            nodelist nodes = el.getchildnodes();
            for (int m = 0, n = nodes.getlength(); m < n; m++) {
              node node = nodes.item(m);
              if (!"ref".equals(node.getnodename()))
                continue;
              string href = node.getattributes()
                  .getnameditem("href").getnodevalue();
              if (ov.url == null) {
                ov.url = href;
              } else {
                if (ov.backup_url == null)
                  ov.backup_url = new arraylist<string>();
                ov.backup_url.add(href);
              }
            }
            if (ov.url != null)
              result.add(ov);
          }
        }
      }
    } catch (ioexception e) {
      e.printstacktrace();
    } catch (saxexception e) {
      e.printstacktrace();
    } catch (parserconfigurationexception e) {
      e.printstacktrace();
    } finally {
      doc = null;
      docbuilder = null;
      docbuilderfactory = null;
    }
    return result;
  }
} 

 三、下载 

请移步#taocode(svn):

    项目地址:http://code.taobao.org/p/oplayer

    svn地址:http://code.taobao.org/svn/oplayer/

 四、参考

  android读写xml(上)——package说明

   各大电视台直播地址

  网络电视直播地址收集 

结束

 本文是新入手macbook pro上写的第一篇文章,诸多不习惯,仍然在一天内成功丢弃鼠标,离ios又近一步了:) 系列文章并不强调用某种技术,但尽可能涉及到多种技术,只有充分了解各种技术才能在适当的时候使用合适的技术。先实现后优化,不必一步到位纠结于每个细节。

以上就是对android vitamio 开发播放在线电视相关资料,后续继续补充。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网