Maria Gheorghe
Maria Gheorghe

Reputation: 629

android mediaplayer get playing url

I'm interested in retrieving the current url or uri of a Media Player. if i run :

String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();

is there a way i get the url this media player is running?

Upvotes: 6

Views: 12122

Answers (1)

Amrut Bidri
Amrut Bidri

Reputation: 6360

class CustomMediaPlayer extends MediaPlayer
{
    String dataSource;

    @Override
    public void setDataSource(String path) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException
    {
        // TODO Auto-generated method stub
        super.setDataSource(path);
        dataSource = path;
    }

    public String getDataSource()
    {
        return dataSource;
    }
}

Use this CustomMediaPlayer class instead to MediaPlayer to get the current url that is passed to the MediaPlayer.

I hope this code might help you in getting the url using:

String url = "http://........"; // your URL here
CustomMediaPlayer customMediaPlayer = new CustomMediaPlayer ();
customMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
customMediaPlayer.setDataSource(url);
customMediaPlayer.prepare(); // might take long! (for buffering, etc)
customMediaPlayer.start();
String url1 = customMediaPlayer.getDataSource();// get your url here

Upvotes: 6

Related Questions