我已经在Android中为媒体播放器创建了演示程序,开始运行我的应用程序时遇到了问题。当我的应用程序运行时,歌曲正在播放但没有播放完整的歌曲,它只是开始并立即完成意味着它只是开始活动并立即进入resume()状态。当歌曲结束播放时,我又重新启动,我的活动歌曲没有从头开始播放,而是从中间开始播放。我在过去的1周里遇到了这个问题,我不知道该如何解决。请任何人帮助我。这是我的代码。非常感谢。

public class Audio_Activity extends Activity
{
    private MediaPlayer mp = null;
    PhoneStateListener phListener;
    int length;
    SharedPreferences prefs;
    ImageView imgVw;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.audio);
        init();
        imgVw.setImageResource(R.raw.teddy_two);

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences. Editor prefsEdit = prefs.edit();

         mp=MediaPlayer.create(Audio_Activity.this,R.raw.issaq_tera_by_vishu);
         Log.e("Song is playing","in  Mediya Player ");

         mp.start();
         mp.setLooping(false);
         System.out.println("Media Plyer Is Start !!!");
         prefsEdit.putBoolean("mediaplaying", true);
         prefsEdit.commit();
         btnChapter.setEnabled(false);

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
        {

            @Override
            public void onCompletion(MediaPlayer mp)
            {
                // TODO Auto-generated method stub
                mp.stop();
                 System.out.println("Media Plyer Is Complete !!!");
                //mp.release();
                prefsEdit.putBoolean("mediaplaying", false);
                prefsEdit.commit();
                btnChapter.setEnabled(true);
                System.out.println("Music is over and Button is enable !!!!!!");
            }
        });


        PhoneStateListener phoneStateListener = new PhoneStateListener()
          {
                @Override
                public void onCallStateChanged(int state, String incomingNumber)

                {
                    if (state == TelephonyManager.CALL_STATE_RINGING)

                    {

                        if(mp!=null)

                        {
                            setPlayerButton(true, false, true);

                            if(mp.isPlaying())
                            {

                                mp.pause();

                            }
                        }

                    } else if(state == TelephonyManager.CALL_STATE_IDLE) {

                    } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {

                    }
                    super.onCallStateChanged(state, incomingNumber);
                }
            };

            TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

             if(mgr != null)
             {

                 mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
             }


    }


    private void setPlayerButton(Boolean play, Boolean pause, Boolean stop){
        btnStartStop.setEnabled(play);
        if(play==true)
            btnStartStop.setEnabled(true);
        else
            btnStartStop.setEnabled(false);

    }



     @Override
        public void onConfigurationChanged(Configuration newConfig)
     {
            super.onConfigurationChanged(newConfig);

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

      }


     @Override
     public void onPause()
     {
         super.onPause();

          SharedPreferences. Editor prefsEdit = prefs.edit();
          boolean isPlaying=prefs.getBoolean("mediaplaying",false);
          if(isPlaying)
          {
              int position = mp.getCurrentPosition();
              Log.e("Current ","Position -> " + position);
              prefsEdit.putInt("mediaPosition", position);
              prefsEdit.commit();
          }
     }


     @Override
        protected void onResume()
        {
            super.onResume();

            System.out.println("Activity is Resume !!!");

            boolean isPlaying=prefs.getBoolean("mediaplaying",false);
            if(isPlaying)
            {
                  int position = prefs.getInt("mediaPosition", 0);
                  mp.seekTo(position);
                  mp.start();

            }

        }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        super.onStop();

        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            if(mp!= null)
            {
                if(mp.isPlaying())
                {
                    mp.pause();
                     System.out.println("Media Player is Pause/Stop click on Back button on Emulator!!!");

                }
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

最佳答案

从歌曲中部开始的问题是因为您将歌曲的当前进度存储在SharedPreferences中,然后在onResume()处从存储在SharedPreferences的进度位置启动播放器

10-05 18:21