vlc-android 获取MediaPlayerEncounteredError,MediaPlayerBuffering等各种事件的响应

        前面有篇文章介绍了调用libvlcjni.so 实现流媒体播放器,在将那篇文章写了之后,开发的项目中又需要更新,其中涉及到了获取MediaPlayerEncounteredError,MediaPlayerBuffering等事件的响应,在网上找了好久都无果,自己花了不少时间终于找到了想要的结果。再次记录出来以防以后淡忘再做重复工作,也希望可以帮助遇到同样问题的道友。

         做vlc-android移植的道友都应该知道,当编译完vlc-android 源码后EventManager.java 类中定义了许多事件,下面是源码一部分:

 

public class EventManager {

    /*
     * Be sure to subscribe to events you need in the JNI too.
     */

    //public static final int MediaMetaChanged                = 0;
    //public static final int MediaSubItemAdded               = 1;
    //public static final int MediaDurationChanged            = 2;
    //public static final int MediaParsedChanged              = 3;
    //public static final int MediaFreed                      = 4;
    //public static final int MediaStateChanged               = 5;

    //public static final int MediaPlayerMediaChanged         = 0x100;
    //public static final int MediaPlayerNothingSpecial       = 0x101;
    //public static final int MediaPlayerOpening              = 0x102;
    //public static final int MediaPlayerBuffering            = 0x103;
    public static final int MediaPlayerPlaying                = 0x104;
    public static final int MediaPlayerPaused                 = 0x105;
    public static final int MediaPlayerStopped                = 0x106;

......
}


 

        可是对于这些事件有很多都被注释掉了,当我们需要被注释掉的事件时,就算把注释拿掉,再调用mEventManager.addHandler(EventManager.getInstance())添加事件之后,也不会在定义的mEventHandler 的handleMessage()中监听到,下面为一个mEventHandler定义的demo:

   private final VideoEventHandler mEventHandler = new VideoEventHandler(this);
    private class VideoEventHandler extends WeakHandler<DtvPlayer>{
    	public VideoEventHandler(DtvPlayer owner) {
            super(owner);
        }
    	@Override
    	public void handleMessage(Message msg) {
    		DtvPlayer activity = getOwner();    		
    		 if(activity == null) return;
    		 switch (msg.getData().getInt("event")) {
    		 case EventManager.MediaPlayerBuffering:
    			 Log.d(TAG, "MediaPlayerBuffering");
    			 break;
             case EventManager.MediaPlayerEncounteredError:
            	Log.d(TAG, "MediaPlayerEncounteredError");
 				break;
				......
             default:
                 Log.e(TAG, String.format("Event not handled (0x%x)", msg.getData().getInt("event")));
                 break;
         }
    		super.handleMessage(msg);
    	}
    }

        那么如何才能够在mEventHandler中监听到我们需要的事件呢,下面将进入主题。

        在libvlcjni.c中有一个静态常量,其中指定了我们目前需要获取哪些事件:

    static const libvlc_event_type_t mp_events[] = {
        libvlc_MediaPlayerPlaying,
        libvlc_MediaPlayerPaused,
        libvlc_MediaPlayerEndReached,
        libvlc_MediaPlayerStopped,
        libvlc_MediaPlayerVout,
        libvlc_MediaPlayerPositionChanged
    };

        你可以将自己需要的事件添加在里面,然后将EventManager中响应的事件注释拿掉,之后重新编译源码就可以再mEventHandler中获取你刚添加的事件了。

(例如:你要想获取MediaPlayerEncounteredError事件,先将libvlc_MediaPlayerEncounteredError添加在mp_events[]静态常量中(注意,这里前面多了libvlc_),然后把EventManager中的public static final int MediaPlayerEncounteredError     = 0x10a;注释拿掉,重新编译源码之后就可以在你得mEventHandler 的handleMessage()中获取到EventManger.MediaPlayerEncounteredError事件)。

        在vlc-android/vlc/lib/event.c中定义了所有事件:

#define DEF( a ) { libvlc_##a, #a, },

typedef struct
{
    int type;
    const char name[40];
} event_name_t;

static const event_name_t event_list[] = {
    DEF(MediaMetaChanged)
    DEF(MediaSubItemAdded)
    DEF(MediaDurationChanged)
    DEF(MediaParsedChanged)
    DEF(MediaFreed)
    DEF(MediaStateChanged)

    DEF(MediaPlayerMediaChanged)
    DEF(MediaPlayerNothingSpecial)
    DEF(MediaPlayerOpening)
    DEF(MediaPlayerBuffering)
    DEF(MediaPlayerPlaying)
    DEF(MediaPlayerPaused)
    DEF(MediaPlayerStopped)
    DEF(MediaPlayerForward)
    DEF(MediaPlayerBackward)
    DEF(MediaPlayerEndReached)
    DEF(MediaPlayerEncounteredError)
    DEF(MediaPlayerTimeChanged)
    DEF(MediaPlayerPositionChanged)
    DEF(MediaPlayerSeekableChanged)
    DEF(MediaPlayerPausableChanged)
    DEF(MediaPlayerTitleChanged)
    DEF(MediaPlayerSnapshotTaken)
    DEF(MediaPlayerLengthChanged)
    DEF(MediaPlayerVout)

    DEF(MediaListItemAdded)
    DEF(MediaListWillAddItem)
    DEF(MediaListItemDeleted)
    DEF(MediaListWillDeleteItem)

    DEF(MediaListViewItemAdded)
    DEF(MediaListViewWillAddItem)
    DEF(MediaListViewItemDeleted)
    DEF(MediaListViewWillDeleteItem)

    DEF(MediaListPlayerPlayed)
    DEF(MediaListPlayerNextItemSet)
    DEF(MediaListPlayerStopped)

    DEF(MediaDiscovererStarted)
    DEF(MediaDiscovererEnded)

    DEF(VlmMediaAdded)
    DEF(VlmMediaRemoved)
    DEF(VlmMediaChanged)
    DEF(VlmMediaInstanceStarted)
    DEF(VlmMediaInstanceStopped)
    DEF(VlmMediaInstanceStatusInit)
    DEF(VlmMediaInstanceStatusOpening)
    DEF(VlmMediaInstanceStatusPlaying)
    DEF(VlmMediaInstanceStatusPause)
    DEF(VlmMediaInstanceStatusEnd)
    DEF(VlmMediaInstanceStatusError)
};
#undef DEF
        其中DEF()将MediaPlayerEncounteredError定义为libvlc_MediaPlayerEncounteredError,当本地代码产生MediaPlayerEncounteredError事件时会将libvlc_MediaPlayerEncounteredError传递给jni,与此同时jni又会传递给java层。不管是本地libvlc_MediaPlayerEncounteredError还是java层MediaPlayerEncounteredError,对于同一个事件被定义的值都是相同的,传输的是同一个消息值。本地代码定义在vlc-android/vlc/include/libvlc_events.h, java代码定义在EventManager.java中。 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值