title: Getting Error in Media Player: SeekTo in wrong state: & start called in state 0
body: <p>i am facing some issue in media player when i use as service. </p>
  
  <p><strong>1.</strong>  If i initialize Media Player Object in Activity as static and further use in service class then its show me this  <code>error (-38, 0)</code> , <code>Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0</code> , <code>start called in state 0</code>.</p>
  
  <p><strong>2.</strong> If i initialize Media Player Object in Service class and user in activity for more 
  action its show's me <code>Null Pointer</code>.</p>
  
  <pre><code>public static MediaPlayer mPlayer = null;
  </code></pre>
  
  <p><strong>play Click Event</strong> </p>
  
  <pre><code>    Audio_Player_Play.setOnClickListener(new OnClickListener()
      {
  
          @Override
          public void onClick(View v)
          {
              // TODO Auto-generated method stub
  
              if (mPlayer == null)
              {
  
                  Intent Play_Intent = new Intent(RDS_Singel_Audio.this,
                          Audio_Player.class);
  
                  startService(Play_Intent);
                  Audio_Player_Play.setVisibility(View.GONE);
                  Audio_Player_Pause.setVisibility(View.VISIBLE);
  
              } else
              {
                  // Play_Streaming();
                  mPlayer.seekTo(mPlayer.getCurrentPosition());
                  mPlayer.start();
                  Audio_Player_Play.setVisibility(View.GONE);
                  Audio_Player_Pause.setVisibility(View.VISIBLE);
              }
          }
      });
  </code></pre>
  
  <p><strong>Pause Click Event</strong></p>
  
  <pre><code>Audio_Player_Pause.setOnClickListener(new OnClickListener()
          {
  
              @Override
              public void onClick(View v)
              {
                  // TODO Auto-generated method stub
                  // Play_Streaming();
                  // checking is medai player running
  
                  Audio_Player_Play.setVisibility(View.VISIBLE);
                  Audio_Player_Pause.setVisibility(View.GONE);
  
                  // checking is audio playing in background
                  if (mPlayer != null &amp;&amp; mPlayer.isPlaying())
                  {
                      mPlayer.pause();
                      /*
                       * Intent Pause_Intent = new Intent(RDS_Singel_Audio.this,
                       * Audio_Player.class); stopService(Pause_Intent);
                       */
                      Audio_Player_Play.setVisibility(View.VISIBLE);
                      Audio_Player_Pause.setVisibility(View.GONE);
                  }
              }
          });
  </code></pre>
  
  <p><strong>OnResume() - OnPause() -OnDistroy()</strong></p>
  
  <pre><code>@Override
      protected void onResume()
      {
          super.onResume();
      }
  @Override
      protected void onPause()
      {
          // cleanUp();
          if (mPlayer != null &amp;&amp; mPlayer.isPlaying())
          {
              mPlayer.release();
              mPlayer = null;
  
          }
  
          super.onPause();
      }
  @Override
      protected void onDestroy()
      {
          cleanUp();
          super.onDestroy();
      }
  
  private void cleanUp()
      {
          if (mPlayer != null)
          {
              mVisualizerView.release();
              mPlayer.release();
              mPlayer = null;
          }
      }
  </code></pre>
  
  <p><strong>Service.Java</strong></p>
  
  <pre><code>public class Audio_Player extends Service
  {
      private static final String TAG = "MyService";
  
      // public static MediaPlayer mPlayer;
      Function f;
      RDS_Singel_Audio sa;
      MediaPlayer mPlayer;
  
      @Override
      public IBinder onBind(Intent intent)
      {
          return null;
      }
  
      @Override
      public void onCreate()
      {
          try
          {
              sa = new RDS_Singel_Audio();
              Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG)
                      .show();
  
              sa.mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
              sa.mPlayer.setDataSource(sa.Audio_URL);
              sa.mPlayer.prepare();// might take long! (for buffering, etc)
              sa.mVisualizerView.link(sa.mPlayer);
              sa.mPlayer.start();
              sa.addLineRenderer();
              sa.mPlayer.setLooping(false); // Set looping
  
              // sa.mPlayer.start();
  
          } catch (Exception e)
          {
              // TODO: handle exception
              Log.d(TAG, "" + e);
          }
  
      }
  
      @Override
      public void onDestroy()
      {
          Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
          Log.d(TAG, "onDestroy");
          super.onDestroy();
          if (sa.mPlayer != null)
          {
              sa.mPlayer.stop();
              sa.mPlayer.release();
  
          }
          sa.mPlayer = null;
  
      }
  
      @Override
      public void onStart(Intent intent, int startid)
      {
          Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
          Log.d(TAG, "onStart");
  
          // mPlayer.setDataSource("http://clients.ncrypted.net/riddimapp/images/audio/4/df9919ccb2880933c795f43d735cf9bc.mp3");
  
      }
  
  }
  </code></pre>
  
  <p>I found some <a href="https://stackoverflow.com/questions/9008770/media-player-called-in-state-0-error-38-0">link</a> about same issue but I don't know how solve it.</p>