MediaPlayer 'prepare();' problem

久未见 提交于 2019-12-04 21:49:19

prepare(); is a blocking operation, if you dont want to block your UI Thread use prepareAsync();. Or use prepare in another Thread

Do all these thing in background thread until media player instance prepared the resource to play and show progress bar upto that time

 //progressDialog
    Thread th=new Thread(new Runnable() {
        @Override
        public void run() {
            MediaPlayer md=new MediaPlayer();
            try {
                md.setDataSource("Path");
                md.prepareAsync();
                md.start();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //send message to handler
        }
    });
    th.start();
    //and then dissmiss dialog in handler class

Update

To know when media player will prepare

    md.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
        //Now your media player is ready to play    
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!