mediaplayer.prepare在android 6中抛出IllegalStateException

时间:2016-10-08 06:22:47

标签: android illegalstateexception

我有一个适用于每个Android版本的代码。 但在android 6抛出

  

IllegalStateException异常

当我打电话时

  

mediaPlayer.prepare();

第一页工作得很好,但是当我滚动到另一个页面时,有时候工作得非常好,有时候它会抛出IllegalStateException并且音频无法播放。

这是我的代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mp3_player);
   pager = (ViewPager) findViewById(R.id.pager);
 Bundle extras = getIntent().getExtras();
 final int id = extras.getInt("ID");
   auMp3 = id + "_1.mp3";
   f = new File(DIR_DATABASE + auMp3);
 Uri uri = Uri.parse(G.DIR_DATABASE + auMp3);
 if (f.exists()) {
     MediaPlayer    mp = MediaPlayer.create(G.context, uri);

    }

 imgPlay.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View arg0) {
            if (f.exists()) {

                if (mp.isPlaying()) {
                    imgPlay.setImageResource(R.drawable.iplay);
                    mp.pause();

                } else if ( !mp.isPlaying()) {


                    imgPlay.setImageResource(R.drawable.ipause);
                    mp.start();


                }


            } else {
                menu.download(auMp3);
            }

        }
    });
adapter = new AuPagerAdapter(aue);

    pager.setAdapter(adapter);
    pager.setCurrentItem(aue.size());

 OnPageChangeListener currentit = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {

        }


@Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
         int i = aue.size() - arg0;
            auMp3 = id + "_" + i + ".mp3";
            f = new File(DIR_DATABASE + auMp3);
           imgPlay.setImageResource(R.drawable.iplay);
            mp.reset();
            if (f.exists()) {

                try {
                    mp.setDataSource(G.DIR_DATABASE + auMp3);

                }
                catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                }
                catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {

                    mp.prepareAsync();

                }
                catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                    Toast.makeText(G.context, "IllegalStateException", 1).show();

                }

            }
        }

0 个答案:

没有答案