title: AndroId MediaPlayer error (1,-4) prepareAsync method
body: <p>I've an error (1,-4) in prepareAsync method. The docs specify that this is a "not correct format of file" (right?) but the file is in mp3 format. In addition the same file was reproduced in an old version of the software when the mediaplayer was called in another Activity.
  I don't know how solve the problem so here I am.</p>
  
  <p>The code for the application is shown below:</p>
  
  <p><strong>SingletonMediaPlayer</strong> will manage a mediaplayer object used as singleton in application</p>
  
  <pre><code>public class SingletonMediaPlayer {
  
      private static SingletonMediaPlayer instance;
      private MediaPlayer mp;
      private int buffer_state;
  
      private SingletonMediaPlayer() {
          Log.d("SMP","Creating new media player");
          this.mp = new MediaPlayer();
          this.buffer_state = 0;
      }
  
      public static SingletonMediaPlayer getInstance() {
          if (instance == null) {
              instance = new SingletonMediaPlayer();
          }
  
          return instance; 
      }
  
      public void play(String path, final TextView tv_messaging){             
          if(this.mp.isPlaying()){
              Log.d("SMP","Player is playing, now I'll stop and reset it");
              this.mp.stop();
              this.mp.reset();
              this.mp.release();
          }
  
          Log.d("SMP","Set audio stream type");
          this.mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
          try {
  
              Log.d("SMP","Set data source");
              this.mp.setDataSource(path);
              Log.d("SMP","Prepare async");
              this.mp.prepareAsync();
              Log.d("SMP","Done!");
              tv_messaging.setText("Connecting to the server...please wait");
          } catch (IllegalArgumentException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              tv_messaging.setText(e.toString());
              Log.e("SMP","IllegalArgumentException");
          } catch (SecurityException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              tv_messaging.setText(e.toString());
              Log.e("SMP","SecurityException");
          } catch (IllegalStateException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              tv_messaging.setText(e.toString());
              Log.e("SMP","IllegalStateException");
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              tv_messaging.setText(e.toString());
              Log.e("SMP","IOException");
          }catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              tv_messaging.setText(e.toString());
              Log.e("SMP","Generic Exception");
          }
      }
  
      public void checkBufferState(final TextView tv_buffer_message){
          final SingletonMediaPlayer self = this;
  
          Log.d("SMP","  Set on prepared listener");
          this.mp.setOnErrorListener(new OnErrorListener(){
  
              @Override
              public boolean onError(MediaPlayer mp, int arg1, int arg2) {
                  // TODO Auto-generated method stub
                  return false;
              }
  
          });
          this.mp.setOnPreparedListener(new OnPreparedListener() {
              public void onPrepared(MediaPlayer mPlayer) {
                  OnBufferingUpdateListener lis = new OnBufferingUpdateListener(){
                      public void onBufferingUpdate(MediaPlayer mPlayer, int percent) {
                          Log.d("SMP","      Mediaplayer ready (preparation done). Inside buffer listener");
                          self.buffer_state = percent;
                          if(tv_buffer_message != null){
                              tv_buffer_message.setText(percent+"%");
                          }
                      }
                  };
  
                  Log.d("SMP","    Mediaplayer ready (preparation done). Installing buffer listener");
                  mPlayer.setOnBufferingUpdateListener(lis);
                  Log.d("SMP","    Mediaplayer ready (preparation done). Starting reproduction");
                  mPlayer.start();
                  Log.d("SMP","    Mediaplayer ready (preparation done). Done!");
              }
          });
      }
  
      public int getBufferState(){
          return this.buffer_state;
      }
  }
  </code></pre>
  
  <p>I'm creating buttons dinamically, when the button is clicked should play an mp3. So, just before the loop used to create buttons I have</p>
  
  <pre><code>final SingletonMediaPlayer mediaPlayer = SingletonMediaPlayer.getInstance();
  mediaPlayer.checkBufferState(tv_sel_ep);
  </code></pre>
  
  <p>and than, for every button, I have</p>
  
  <pre><code>button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          mediaPlayer.play(link, Text_view_used_to_show_error);
      }
  });
  </code></pre>
  
  <p><strong>Log</strong></p>
  
  <pre><code>02-15 14:35:24.165: D/dalvikvm(1415): GC_EXTERNAL_ALLOC freed 58K, 52% free 2633K/5379K, external 391K/517K, paused 87ms
  02-15 14:35:25.705: D/NetworkActivity(1415): Starting SelectEpisodeActivity
  02-15 14:35:25.995: D/SMP(1415): Creating new media player
  02-15 14:35:26.005: D/SMP(1415):   Set on prepared listener
  02-15 14:35:28.745: W/KeyCharacterMap(1415): No keyboard for id 0
  02-15 14:35:28.745: W/KeyCharacterMap(1415): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
  02-15 14:35:28.745: I/qqqqqqqqqqqqq(1415): qqqqqqqqqqqqqqqqqqqq
  02-15 14:35:29.235: D/dalvikvm(1415): GC_CONCURRENT freed 307K, 53% free 2669K/5639K, external 477K/989K, paused 8ms+12ms
  02-15 14:35:30.275: D/NetworkActivity(1415): Starting SelectEpisodeActivity
  02-15 14:35:30.415: D/SMP(1415):   Set on prepared listener
  02-15 14:35:32.485: D/SMP(1415): Set audio stream type
  02-15 14:35:32.485: D/SMP(1415): Set data source
  02-15 14:35:32.785: D/SMP(1415): Prepare async
  02-15 14:35:32.785: D/SMP(1415): Done!
  02-15 14:35:32.885: W/MediaPlayer(1415): info/warning (1, 26)
  02-15 14:35:32.885: I/MediaPlayer(1415): Info (1,26)
  02-15 14:35:32.885: E/MediaPlayer(1415): error (1, -4)
  02-15 14:35:32.885: E/MediaPlayer(1415): Error (1,-4)
  </code></pre>
  
  <p><strong>EDIT: New log after Dave answer</strong></p>
  
  <pre><code>02-16 14:35:11.265: D/dalvikvm(12723): GC_EXTERNAL_ALLOC freed 59K, 52% free 2633K/5379K, external 391K/517K, paused 133ms
  02-16 14:35:13.265: D/NetworkActivity(12723): Starting SelectEpisodeActivity
  02-16 14:35:13.405: D/SMP(12723): Creating new media player
  02-16 14:35:13.415: D/SMP(12723):   Set on prepared listener
  02-16 14:35:17.365: D/SMP(12723): Set audio stream type
  02-16 14:35:17.365: D/SMP(12723): Set data source
  02-16 14:35:17.435: D/SMP(12723): Prepare async
  02-16 14:35:17.435: D/SMP(12723): Done!
  02-16 14:35:17.445: W/MediaPlayer(12723): info/warning (1, 26)
  02-16 14:35:17.445: E/MediaPlayer(12723): error (1, -4)
  02-16 14:35:17.465: I/MediaPlayer(12723): Info (1,26)
  02-16 14:35:17.465: E/MediaPlayer(12723): Error (1,-4)
  </code></pre>
  
  <p>Thanks for help</p>