How to set proxy on MediaPlayer

2019-04-02 06:30发布

问题:

In my music stream program, how can I set proxy on android.media.MediaPlayer class for stream a link file through the proxy network ? I know about NTCreditional, UsernamePasswordCreditional, Proxy-Authorization Header and etc in HttpClient, URLConnection. But I can not set proxy on android.media.MediaPlayer, how can I do it?

Thanks for your advance :)

回答1:

Unfortunately MediaPlayer API doesn't provide a ready-to-use method for proxy setting at the moment.

how can I do it?

There is a possible workaround but quite dirty:

  1. Download the media contents from the remoteUrl http://remotehost:80/music, using whatever technology you familiar with, socket, httpClient and etc. and handle proxy authentication properly here.
  2. Open a socket locally (on your mobile device) and write the downloaded data to this socket's OutputStream, what we actually want is republish the downloaded contents to a localUrl http://localhost:8081/music running on our mobile device.
  3. Feed this localUrl to your MediaPlayer: mediaPlayer.setDataSource(localURL); instead of mediaPlayer.setDataSource(remoteURL);.

Related Materials:

  • Similar SO discussion: Audio stream buffering
  • StreamProxy source code from npr-android-app

Hope this make sense.