CallBack from MediaPlayer

╄→尐↘猪︶ㄣ 提交于 2020-01-05 05:52:10

问题


I have a MediaPlayer and an audio file, which I need to play, also I have an ArrayList with the certain seconds of this audio file, on this seconds while audio is playing, I need to switch pages in the ViewPager. How to get a CallBack from the MediaPlayer in these certain moments of the time? or maybe you will recommend some other way how is possible to do it.

What I have right now is the method, where I use CountDownTimer, which change pages in ViewPager, but it works wrong and it doesn't take into the account fact that an audio track can be stop and then resume.

 public void startPlayingLabels() {

        mPlayer = new MediaPlayer();

        try {
            String mFileName = CameraFragment.mAudioFolder + "/" + ViewActivity.parentName + ".3gp";
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();

            for (int i=0; i<zeroLabelPosition.size()-1; i++) {

                final int finalI = i;
                new CountDownTimer(Integer.parseInt(String.valueOf(Integer.parseInt((String) zeroTime.get(finalI + 1)) - Integer.parseInt((String) zeroTime.get(finalI)))), 1000) {
                    public void onTick(long millisUntilFinished) {
                    }

                    public void onFinish() {
                        mPager.setCurrentItem(Integer.parseInt(String.valueOf(zeroLabelPosition.get(finalI))));
                    }
                }.start();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

回答1:


Android madiaplayer does not have any timeline update, thats why you can write your own, or you can use something like this.

CountDownTimer timer;
int timeStampIterator;
int[] timeStamp = new int[5]; // your own time stamps when view must be switched

void play() { 
   mPlayer.start();
    timer = new CountDownTimer(mPlayer.getDuration() - mPlayer.getCurrentPosition(), 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            int timeSpends = mPlayer.getDuration() - mPlayer.getCurrentPosition();
            if (timeSpends == timeStamp[timeStampIterator]) {
                //switchToNExtSlide();
                timeStampIterator++;
            }
        }

        @Override
        public void onFinish() {

        }
    }.start();
}

void pause() {
    mPlayer.pause();
    timer.cancel();
    timer = null;
}

void stop() {
    mPlayer.stop();
    timer.cancel();
    timer = null;
    timeStampIterator = 0;
} 



回答2:


you can also use handler for it like following

Handler handler = new Handler();

       Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();// change hear page for view pager, because it call after every 1 second
            }
        };
        handler.postDelayed(notification, 1000);


来源:https://stackoverflow.com/questions/37812067/callback-from-mediaplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!