Seting the duration of Media Player media in Android

故事扮演 提交于 2019-12-11 08:26:33

问题


I want to set the time duration for MediaPlayer. Here is my code:

MediaPlayer play = MediaPlayer.create(getApplicationContext(),R.raw.beepsound);
play.start();

回答1:


You can use Timer which will be updated every 1000 milliseconds. Then set time to a TextView or what ever you want.

if (player != null) {
    player.start();
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (player != null && player.isPlaying()) {
                        tv.post(new Runnable() {
                            @Override
                            public void run() {
                                tv.setText(player.getCurrentPosition());
                            }
                        });
                    } else {
                        timer.cancel();
                        timer.purge();
                    }
                }
            });
        }
    }, 0, 1000);
}


来源:https://stackoverflow.com/questions/16052851/seting-the-duration-of-media-player-media-in-android

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