Android中,用MediaPlayer播放在线mp3链接时,能不能带上cookie?

服务器上的资源必须要有cookie才能访问。下载资源我可以带上cookie,但是利用mediaplayer在线播放时怎么带上cookie呢?

该回答引用ChatGPT

在Android中,使用MediaPlayer播放在线mp3链接时,可以通过设置HTTP请求头来添加cookie信息。


您可以使用setRequestProperty()方法来设置HTTP请求头,如下所示:

String cookie = "your_cookie_string";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.setRequestProperty("Cookie", cookie);
mediaPlayer.prepareAsync();

在上面的代码中,将您的cookie字符串存储在变量cookie中,然后将其作为HTTP请求头的一部分添加到MediaPlayer中。


注意,这只适用于使用HTTP协议的资源。如果您的资源使用HTTPS协议,则需要使用SSL套接字工厂设置SSL上下文来添加cookie。