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 inte易做图ce 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){
补充:移动开发 , Android ,