在前台播放视频时,我需要持续监控 VideoView 的缓冲区百分比。我创建了一个 AsyncTask 类并将 VideoView 传递给该类。但是,当我尝试访问缓冲区百分比时,它总是返回 0。我在 Main 活动的 onCreate 方法上初始化并开始播放视频。我的代码:
public class MainActivity extends Activity {
static MediaController mc;
static VideoView vw;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mc = new MediaController(this);
vw = (VideoView) findViewById(R.id.videoView);
vw.setVideoPath("http://173.45.164.105:1935/live/myStream/playlist.m3u8");
vw.requestFocus();
vw.start();
// Execute async task for stream error
ConnCheck cc = new ConnCheck();
cc.execute();
Log.i("REACHED MAIN", "Started Playing");
}
}
class ConnCheck extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
while (MainActivity.vw.isPlaying() == true) {
Log.i("HEY", "DOING STUFF");
}
return null;
}