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

前言

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

 

声明
  欢迎转载,但请保留文章原始出处:) 
    博客园:http://www.cnblogs.com
    农民伯伯: http://over140.cnblogs.com 




 

正文

一、目标

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

 

 

、主要代码

电视台的地址目前是存在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/ 

 

四、Vitamio公告

     正式建立Vitamio开发者联盟QQ群!群号为: 246969281
      注意: 目前仅接受已经开发基于Vitamio产品的开发者申请加入 ,申请理由请填写产品的名称和链接!

    注意更新至2012-07-09发布的新版SDK:这里。

 

、参考

Android读写XML(上)——package说明

  各大电视台直播地址

网络电视直播地址收集 


结束

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值