问题描述
我想听听我创建缓冲的开始和结束时获得一个媒体播放器的OnInfoListener。
I'm trying to listen to OnInfoListener of a mediaplayer I've created to get when buffering starts and ends.
由于某些原因,事件永远不会触发。
For some reason the event never fires.
这是我的初始化code
This is my initializing code
private void initPlayer(Surface s){
mPlayer = new MediaPlayer();
mPlayer.setSurface(s);
mPlayer.setOnCompletionListener(this);
mPlayer.setOnErrorListener(this);
mPlayer.setOnInfoListener(this);
mPlayer.setOnPreparedListener(this);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(file_url);
mPlayer.prepare();
} catch (IllegalArgumentException e) {
OnError(PLAYER_ERRORS.player_not_loaded, e.getMessage() + "");
} catch (SecurityException e) {
OnError(PLAYER_ERRORS.player_not_loaded, e.getMessage() + "");
} catch (IllegalStateException e) {
OnError(PLAYER_ERRORS.player_not_loaded, e.getMessage() + "");
} catch (IOException e) {
OnError(PLAYER_ERRORS.no_connection, e.getMessage() + "");
}
}
这是我的听众
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if(what == MediaPlayer.MEDIA_INFO_BUFFERING_START)
{
OnEvent(PLAYER_EVENT.buffering_start);
}
else if(what == MediaPlayer.MEDIA_INFO_BUFFERING_END)
{
OnEvent(PLAYER_EVENT.buffering_end);
OnGotDuration(getDuration());
}
return what == MediaPlayer.MEDIA_INFO_BUFFERING_START || what == MediaPlayer.MEDIA_INFO_BUFFERING_END;
}
有人可以帮助我了解为什么会出现这种情况?
谢谢
Can someone please help me understand why is this happening?Thanks
推荐答案
那么显然它在Android的一个已知的bug。
当播放HLS流,它只是永远不会调用OnInfoListener或OnBuffering。
Well apparently it's a known bug in Android.When playing HLS stream it's just never calls OnInfoListener or OnBuffering.
了解更多详情
我的所谓的解决方案是创建一个线程现在沿玩家的侧运行,并检查其进展不时以确定回放被停止 - 假定这意味着玩家缓冲
My so called solution was to create a thread now runs along side of the player, and checking its progress from time to time to determine if the playback is stopped - assuming that means the player is buffering.
下面是我的code对他们来说,永远需要它
Here's my code for whom ever wants it
private class BufferingThread extends Thread{
WeakReference<NewDefaultPlayer> player;
long currentPos, lastSecond;
boolean isBuffering, alreadyBuffering;
private Object locker = new Object();
public BufferingThread(NewDefaultPlayer player) {
this.player = new WeakReference<NewDefaultPlayer>(player);
}
private Runnable BufferStart = new Runnable() {
public void run() {
player.get().OnEvent(PLAYER_EVENT.buffering_start);
}
};
private Runnable BufferStop = new Runnable() {
public void run() {
player.get().OnEvent(PLAYER_EVENT.buffering_end);
}
};
@Override
public void run() {
super.run();
while(true)
{
synchronized (locker) {
currentPos = player.get().getCurrentPosition();
isBuffering = currentPos == 0 ? true : currentPos <= lastSecond;
lastSecond = (int) currentPos;
if(alreadyBuffering){
if(!isBuffering)
{
if(player.get().getPlayerState().equals(PLAYER_STATES.IS_PLAYING))
{
alreadyBuffering = isBuffering;
TvinciSDK.getMainHandler().post(BufferStop);
}
}
} else {
if(isBuffering){
alreadyBuffering = isBuffering;
TvinciSDK.getMainHandler().post(BufferStart);
}
}
try {
sleep(1500);
} catch (InterruptedException e) {
break;
}
}
}
}
}
//Stop the thread if the user has stopped the video
private void handleBufferingWorkaround(boolean work){
if(bThread == null)
bThread = new BufferingThread(this);
if(work){
if(!bThread.isAlive())
bThread.start();
}
else if(bThread.isAlive())
{
bThread.interrupt();
bThread = null;
}
}
希望它能帮助
这篇关于OnInfoListener不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!