1

So I have to access a media file which is on the server. I have to add access token to header so I can access the file. My goal is to play the media. The usual code is like this:

//for audio
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();

//for video
videoView.setVideoURI(Uri.parse(url));
videoView.start();

Now consider that the url that I'm calling needs an OAuth2 authentication, which literally means adding an access-token to the header of the request. Is this possible by any means in Android?

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
SSP
  • 431
  • 4
  • 16

1 Answers1

0

You can use a setDataSource override to include custom headers, so I believe this will work:

Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Bearer " + accessToken):
mediaPlayer.setDataSource(this, url, headers);
Gary Archer
  • 22,534
  • 2
  • 12
  • 24