Search code examples
androidoauth-2.0android-mediaplayerandroid-videoview

Stream video/audio from an endpoint protected by OAuth2, Android?


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?


Solution

  • 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);