MediaPlayer stream from url
<p>I have a simple app and I want to play a mp3 from a url.</p>

<p>when I add this code to OnCreate, the app crashes at opening:</p>

<pre><code>try {
        mediaPlayer.setDataSource("http://www.freesfx.co.uk/rx2/mp3s/9/10183_1367780535.mp3");
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
</code></pre>

<p>Why? What's wrong with this part of the code?</p>

<p>Complete code: </p>

<pre><code>  public class MediaStreamTest extends AppCompatActivity {
ImageButton bPlay;
ImageButton bStop;
MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_media_stream_test);

    bPlay = (ImageButton)findViewById(R.id.btnPlay);
    bStop = (ImageButton)findViewById(R.id.btnStop);
    try {
        mediaPlayer.setDataSource("http://www.freesfx.co.uk/rx2/mp3s/9/10183_1367780535.mp3");
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }

    bPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mediaPlayer.isPlaying()) {
                bPlay.setBackgroundResource(R.drawable.ic_play_arrow_24dp);
                mediaPlayer.pause();
            }
            else {
                bPlay.setBackgroundResource(R.drawable.ic_pause_24dp);
                mediaPlayer.start();
            }
        }
    });
}
</code></pre>

<p>}</p>