android 序列播放器

下面的这个例子是运行在anroid平台上的。经过在anroid 3.1平台上测试通过。
通常有这样的业务需求:在播放一段视频之前加入一段广告,这个段广告可能是图片的也能是视频的,或者是其他的什么东西。
说白了就是序列播放器,就是在一个显示区域,不同的时间段播放不同类型的内容。
要做到这点,首先需要有2个定时器:一个是现实总的播放时间。一个是单个序列项的播放时间。
为了灵活期间,序列播放器的序列内容可以通过配置文件读取生成.
配置文件的格式如下:
文件清单 01 sequence.xml

<?xml version="1.0" encoding="utf-8"?>
<seqRoot during="20">
 <item type="image" during="5" uri="1.png"/>
 <item type="video" during="10" uri="aa.mp4"/>
 <item type="image" during="5"  uri="2.png"/>
</seqRoot>

 

构造一个实体类:
文件清单 2

class  Item{
 private String uri ;
 private String type;
 private int during; 
   //getter setter 方法省略
}

 

通过构造一个类然并解析然后包装。
文件清单 3

class ItemMgr{
 private ArrayList<Item> seqItems = new ArrayList<Item>();
 int totalDuring;
 public static ArrayList<Item> parseFile(String filePath){
  DocBuilderFactory builderFactory = null ;
  DocBuilder docBuilder = null;
  InputStream in = null;
  Document doc = null;
 try{
   builderFactory = DocBuilderFactory.newInstace();
   docBuilder = bilderFactory.newDocumentBuilder();
   in = new FileInputStream(new File(filePath));   
   doc = docBuilder.parser(in);
   Element root = doc.getDocumentElement();
   totalDuring = Integer.parsInt(root.getAttribute("during")*1000);
   NodeList childList = root.getNodeList();
   Node child ;
   Item item = new Item();
   for(int i = 0 ;i<childList.length;i++){     
    child = childList.get(i);
    if(child.getNodeType==Node.ELEMENT_TYPE){
      item.setUri(child.getAttribute("uri"));
      item.setDuring(Integer.parse(child.getAttribute("during"))*1000);
      item.setType(child.getAttribute("type"));
      seqItems.add(item);
    }     
   }
   
  }catch(Exception e){
   e.printStack();
  }finally{
   if(in!=null)
     try{
     in.close()
     }catch(Exception e){
      e.printStack();
     }
  }
  
  return  seqItems ;
 }
}


为了增加播放类别的可扩展性,用了简单工程模式,来生产播放不同类别的文件。

清单4:播放器接口

public interface  Player {
 play(String uri);
 stop();
 View getView();
}


清单 5:播放器工厂

public class PlayerFactory{
 private Player player = null;
 private Static final String IMAGE = "image";
 private Static final String VIDEO = "video";
 
 public Player getPlayer(String type,Context context){
  if(type.equalsIgnoreCase(IMAGE)){
   player = new ImagePlayer(context);
  }esle if(type.equalsIgnoreCase(VIDEO)){
    player = new VideoPlayer(context);
  }else{
   player = null;
  }
  return player;
 }
}


public class ImagePlayer implements Player {
 privatet ImageView iv ;
 public ImagePlayer(Context context){
  iv = new ImageView(context);
 }
 
 public void play(String uri){
  iv.setImageBitmap(BitmapFactory.decodeFile(uri));
 }
 
 public View getView(){
  return iv;
 }
 
 public void stop(){
  iv.setImageBitmap(null);
 }
}


 


//同理省略了VideoPlayer.

清单 6:

创建主activity

public SequenceActivity extends Activity{
 private int timeout;//每一项播放时长
 private int currentIndex;//当前播放的项
 private int during;//总的序列播放时间
 private TextView title;//显示倒计时:还有多少秒
 private LinearLayout linearLayout;//播放区域
 private Player player;
 private Handler handler = new Handler();
 private ArrayList<Item> seqItems = new ArrayList<Item>();
 private Runnable countDown  = new Runnable{
  String str = "该剩余:"+during/1000+"秒";
  @Override 
  public void run(){
    during=during-1000;
    str = "还剩余:"+during/1000+"秒";
    title.setText(str);
    startHandle.postDelayed(this, 1000);
    if(during==0){
     Message msg = new Message();
     msg.what = 2;
     tv.setVisibility(View.gone);
     playEndHander.post(msg); 
    }
  }
 };
 
 private Handler playEndHandler = new Handler(){
 @Override
 public void handleMessage(Message msg){
  if(msg.what==2){
   player.end();
  }
 }
 };
 
 private Handler timeoutHandler = new Handler(){
  @Override
  public void handleMessage(Message msg){
   if(msg.what==1){
      if(currentIndex==seqItems.size())
         return;
      currentIndex++;
      player = PlayerFactory(seqItems.get(currentIndex).getType());
      linearLayout.removeAllViews();
      player.play(seqItems.get(currentIndex).getType());
      linearLayout.addView(player.getView());
      startTimer(seqItems.get(currentIndex).getDuring());
   }
  }
 };
 
 
 @Override
 public void onCreate(Bundle onSavedInstance){
  title = (TextView)findViewById(R.id.title);
  linearLayout = (LinearLayout)findViewById(R.id.linearLyout);
  
  seqItems = ItemMgr.parseFile(file);
  during = itemMgr.during();
  currentIndex = 0;  
  
  play();
 }
 
 private void startTimer(int timeout){
  this.timeout = timeout;
  new Thread(Timer).start();
 }

 private Runnable Timer = new Runnable(){     
   public void run(){
     try{
       new Thead().sleep(timeout);
       Message msg = new Message();
       msg.what = 1;
       timeoutHandler.post(msg);
      }catch(Exception e){e.printStack()}
   }
 }
 
 private void play(){
  linearLayout.removeAllViews();
  player = playerFacory(seqItems.get(currentIndex).getType());
  player.play(seqItems.get(currentIndex).getUri());
  linearLayout.addView(player.getView());
    handler.post(countDown);
    startTimer(seqItems.get(currentIndex).getDuring());
 }
}

上面代码详细的介绍了序列播放器要用到的知识:线程通信,消息发送。希望对诸位能有所帮助

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值