title: Media Player Fragement Crashes
body: <p>You may have seen that I have had a problem with creating a Media Player a few months back. Well I am trying to get to the next stage by getting it to work in a fragment but the app keeps on stopping/crashing on me. Can someone help me please? Here is my activity/fragment (code-behind) class</p>
  
  <pre><code>public class MediaPlayerNew extends Fragment implements 
  android.media.MediaPlayer.OnCompletionListener, 
  android.media.MediaPlayer.OnPreparedListener,
  android.media.MediaPlayer.OnErrorListener,          
  android.media.MediaPlayer.OnBufferingUpdateListener{
  
  private MediaPlayer mp = null;
  private String TAG = getClass().getSimpleName();
  private Button play;
  private Button pause;
  private Button stop;
  private TextView txtVStatus;
  
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.activity_player, container, false);
  
      return view;
  }
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
      play = (Button) getActivity().findViewById(R.id.btnPlay);
      pause = (Button) getActivity().findViewById(R.id.btnPause);
      stop = (Button) getActivity().findViewById(R.id.btnStop);
      //status = (TextView) findViewById(R.id.txtVStatus);
  
  pause.setEnabled(false);
  stop.setEnabled(false);
  
  play.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
          play();
      }
  });
  
  pause.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
          pause();
      }
  });
  
  stop.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
          stop();
      }
  });
  }
  
  private void play() {
      //HTTP to BCRFM STREAM
      Uri myUri = Uri.parse("http://37.187.193.36:8002");
      try {
          if (mp == null) {
              this.mp = new android.media.MediaPlayer();
          } else {
              mp.stop();
              mp.reset();
          }
          mp.setDataSource(myUri.toString()); // Go to Initialized state)
          mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
          mp.setOnPreparedListener(this);
          mp.setOnBufferingUpdateListener(this);
  
          mp.setOnErrorListener(this);
          mp.prepareAsync();
  
          Log.d(TAG, "LoadClip Done");
      } catch (Throwable t) {
          Log.d(TAG, t.toString());
      }
  }
  
  @Override
  public void onPrepared(android.media.MediaPlayer mp) {
      Log.d(TAG, "Stream is prepared");
      final TextView textViewToChange = (TextView) getActivity().findViewById(R.id.txtVStatus);
      textViewToChange.setText("Welcome to BCRfm");
      mp.start();
      play.setEnabled(false);
      pause.setEnabled(true);
      stop.setEnabled(true);
  
  }
  
  private void pause() {
      mp.pause();
      final TextView textViewToChange = (TextView) getActivity().findViewById(R.id.txtVStatus);
      textViewToChange.setText("You have paused your feed to BCRfm");
      play.setEnabled(true);
      pause.setEnabled(false);
      stop.setEnabled(true);
  }
  
  private void stop() {
      mp.stop();
      final TextView textViewToChange = (TextView) getActivity().findViewById(R.id.txtVStatus);
      textViewToChange.setText(" ");
      play.setEnabled(true);
      pause.setEnabled(false);
      stop.setEnabled(false);
  
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      stop();
  }
  
  public void onCompletion(android.media.MediaPlayer mp) {
      stop();
  }
  
  public boolean onError(android.media.MediaPlayer mp, int what, int extra) {
      StringBuilder sb = new StringBuilder();
      sb.append("Media Player Error: ");
      switch (what) {
          case android.media.MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
              sb.append("Not Valid for Progressive Playback");
              break;
          case android.media.MediaPlayer.MEDIA_ERROR_SERVER_DIED:
              sb.append("Server Died");
              break;
          case android.media.MediaPlayer.MEDIA_ERROR_UNKNOWN:
              sb.append("Unknown");
              break;
          default:
              sb.append(" Non standard (");
              sb.append(what);
              sb.append(")");
      }
      sb.append(" (" + what + ") ");
      sb.append(extra);
      Log.e(TAG, sb.toString());
      return true;
  }
  public void onBufferingUpdate(android.media.MediaPlayer mp, int percent) {
      Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
  }
  }
  </code></pre>