带有API网址的android mediaplayer [英] android mediaplayer with api url

查看:74
本文介绍了带有API网址的android mediaplayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过具有某些API链接的android mediaplayer运行媒体(因此url末尾没有.mp3),

I am trying to run media through android mediaplayer with some API link (thus doesn't have .mp3 in the end of url) as follows:

android代码:

android code :

headers.put("token", token_value);
myPlayer.setDataSource(
                    getApplicationContext(),
                    Uri.parse(url),//url : http://basedomain.com/api/getmp3
                    headers
            );

后端laravel代码:

backend laravel code :

$song_path = "abc.mp3";   
$path = storage_path().DIRECTORY_SEPARATOR."somefolder".DIRECTORY_SEPARATOR.$song_path;
$response = new BinaryFileResponse($path);
return $response;

我从laravel代码中获得了二进制响应(通过邮递员进行了测试),但是android代码没有从laravel代码中播放音乐.

I am getting the binary response from laravel code(tested through postman), however android code is not playing the music from laravel response.

android问题列表:

android issue list :

1)W/MediaPlayer:无法打开 http://basedomain.com/api/getmp3:java.io.FileNotFoundException:没有内容提供者: http://basedomain.com/api/getmp3

1) W/MediaPlayer: Couldn't open http://basedomain.com/api/getmp3: java.io.FileNotFoundException: No content provider: http://basedomain.com/api/getmp3

2)E/MediaPlayer:错误(1,-2147483648)

2) E/MediaPlayer: error (1, -2147483648)

推荐答案

我终于找到了此问题的原因.我的后端laravel api调用是POST请求.更改相同的获取请求对我有用. 将请求从POST更改为GET也不是问题,因为我的请求参数是通过标头发送的. (这样做是因为android mediaplayer的setDataSource方法仅接受标头,而不接受主体参数).

I finally found the reason behind this issue. My backend laravel api call was a POST request. Changing the same to Get request worked for me. Also changing request from POST to GET was not an issue as my request parameters were sent through the header. (This was done because android mediaplayer's setDataSource method only accepts headers and not body parameters).

简而言之,Android Mediaplayer期望我们仅使用GET请求.

In short android mediaplayer is expecting us to use GET request only.

这篇关于带有API网址的android mediaplayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆