title: How to stream music from url with Java - Android
body: <p>I'm totally new to Android Development and to Android devices in general, so I don't know how things are working here.</p>
  <p>I want to make an app that will stream music from my url and still playing the song after I minimize the application.</p>
  <p>I searched my question but a lot of answers were for mp3 songs or other types, but my url is from a live radio so it isn't one song only.</p>
  <p>One of the answers that I found and were good for my problem was <a href="https://stackoverflow.com/questions/8907068/android-how-to-stream-music-using-default-player">this</a> and uses this code:</p>
  <pre><code>Uri myUri = Uri.parse(&quot;your url here&quot;);
  Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
  intent.setDataAndType(myUri, &quot;audio/*&quot;); 
  startActivity(intent);
  </code></pre>
  <p>This prompt me to choose a music player.</p>
  <p>Is there any way to just press my &quot;play&quot; button and hear the music?</p>
  <p>In my iOS app I use this code and I can start and stop the streaming music whenever I want without an external player:</p>
  <pre><code>func prepareToPlay() {
          
          let url = URL(string: &quot;myUrl&quot;)
          
          playerItem = AVPlayerItem(url: url!)
          
          player = AVPlayer(playerItem: playerItem)
            
          player?.play()
  }
  </code></pre>
  <p>Thanks in advance</p>
  <p><strong>EDIT</strong></p>
  <p>After suggested in comments and answer I tried to play it with <code>MPlayer</code>, I made a function and I called it when I tapped my button like this:</p>
  <pre><code>public void playM() {
      String url = &quot;http://android.programmerguru.com/wp-content/uploads/2013/04/hosannatelugu.mp3&quot;;
  
      mPlayer = new MediaPlayer();
      mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      try {
          mPlayer.setDataSource(url);
      } catch (IllegalArgumentException e) {
          Toast.makeText(getApplicationContext(), &quot;You might not set the URI correctly!&quot;, Toast.LENGTH_LONG).show();
      } catch (SecurityException e) {
          Toast.makeText(getApplicationContext(), &quot;You might not set the URI correctly!&quot;, Toast.LENGTH_LONG).show();
      } catch (IllegalStateException e) {
          Toast.makeText(getApplicationContext(), &quot;You might not set the URI correctly!&quot;, Toast.LENGTH_LONG).show();
      } catch (IOException e) {
          e.printStackTrace();
      }
      try {
          mPlayer.prepare();
      } catch (IllegalStateException e) {
          Toast.makeText(getApplicationContext(), &quot;You might not set the URI correctly!&quot;, Toast.LENGTH_LONG).show();
      } catch (IOException e) {
          Toast.makeText(getApplicationContext(), &quot;You might not set the URI correctly!&quot;, Toast.LENGTH_LONG).show();
      }
      mPlayer.start();
  }
  </code></pre>
  <p>But I get an error (the fourth message) and I saw in the logs this:</p>
  <blockquote>
  <p>Unable to create media player</p>
  <p>prepareAsync called in state 1, mPlayer(0x0)</p>
  <p>start called in state 1, mPlayer(0x0)</p>
  <p>error (-38, 0)</p>
  </blockquote>