Android MediaPlayer 从 PHP 重定向流式传输不起作用!

发布于 2024-09-15 16:20:21 字数 839 浏览 6 评论 0原文

我工作的公司正在开发一个 Android 应用程序,可以播放网络上 URL 的视频文件。这 视频 URL 是 PHP 脚本的一个参数,该脚本对其进行正确编码并重定向到编码视频,如下所示:

header('Content-Type: video/'.$format);
header('Location:'.$output_video);

其中 $output_video 是编码视频的 URL(如果我们在浏览器),$format 是视频格式。

但是当我尝试执行 MediaPlayerDemo_Video 从使用流模式的 API 演示中,我收到如下错误:

MediaPlayer Command PLAYER INIT completed with an error or info PVMFErrCorrupt
MediaPlayer error (1. -10)
MediaPlayer Error (1.-10)

如果我们在 PHP 脚本中对 URL 和格式进行硬编码,它也不起作用,但会出现不同的错误:

MediaPlayer info/warning (1. 28)
MediaPlayer Info (1 .28)

有人有吗关于如何解决这个问题的想法?

提前致谢!

The company I work for is developing an Android App that plays a video file from a URL on web. The
video URL is a parameter for a PHP script that encode it properly and redirects to the encoded video as shown below:

header('Content-Type: video/'.$format);
header('Location:'.$output_video);

Where $output_video is the URL to the encoded video (it works if we use this URL in the browser) and $format is the video format.

But when I try executing the MediaPlayerDemo_Video from the API Demos using the streaming mode, I get an error like this:

MediaPlayer Command PLAYER INIT completed with an error or info PVMFErrCorrupt
MediaPlayer error (1. -10)
MediaPlayer Error (1.-10)

If we hard-code the URL and format in the PHP script, it also does not work out, but with a different error:

MediaPlayer info/warning (1. 28)
MediaPlayer Info (1 .28)

Does anyone have an idea on how to workaround this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洛阳烟雨空心柳 2024-09-22 16:20:21

我遇到了同样的问题。结果 android MediaPlayer 无法处理重定向,因此您必须找到 php 脚本将您重定向到的位置,并按照 Jeorgesys 的解释为其提供 rtsp URL。

我能够通过执行 HttpGet 并且不遵循任何重定向,然后从“位置”Http 标头中提取 rtsp url 来解决。如果您有多个重定向,则会遇到更多麻烦,但幸运的是,就我而言,我只需要担心单个重定向。

public static String resolveRedirect(String url) throws ClientProtocolException, IOException {
    HttpParams httpParameters = new BasicHttpParams();
    HttpClientParams.setRedirecting(httpParameters, false);

    HttpClient httpClient = new DefaultHttpClient(httpParameters);      
    HttpGet httpget = new HttpGet(url);
    HttpContext context = new BasicHttpContext();

    HttpResponse response = httpClient.execute(httpget, context);

    // If we didn't get a '302 Found' we aren't being redirected.
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY)
        throw new IOException(response.getStatusLine().toString());

    Header loc[] = response.getHeaders("Location");
    return loc.length > 0 ? loc[0].getValue() : null;
}

I encountered this same problem. Turns out the android MediaPlayer won't handle redirects, so you have to find where the php script is redirecting you and give it the rtsp URL as Jeorgesys explained.

I was able to solve by performing a HttpGet and NOT following any redirects, then pulling the rtsp url from the 'Location' Http header. If you have multiple redirects, you'll have a little more trouble, but luckily in my case I only have to worry about a single redirect.

public static String resolveRedirect(String url) throws ClientProtocolException, IOException {
    HttpParams httpParameters = new BasicHttpParams();
    HttpClientParams.setRedirecting(httpParameters, false);

    HttpClient httpClient = new DefaultHttpClient(httpParameters);      
    HttpGet httpget = new HttpGet(url);
    HttpContext context = new BasicHttpContext();

    HttpResponse response = httpClient.execute(httpget, context);

    // If we didn't get a '302 Found' we aren't being redirected.
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY)
        throw new IOException(response.getStatusLine().toString());

    Header loc[] = response.getHeaders("Location");
    return loc.length > 0 ? loc[0].getValue() : null;
}
情定在深秋 2024-09-22 16:20:21

响应是您尝试在 MediaPlayer 中流式传输什么文件,您的 URL 必须类似于 ::

rtsp://v1.cache5.c.youtube.com/CjYLENy73wIaLQkUvSkxA_7UKxMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYIPXxZHky7m5Rgw=/0/0/0/video.3gp

(尝试使用此网址)

使用 rtsp 协议 以及具有正确 Android 支持的编解码器

the response is what file are you tryin to stream in your MediaPlayer, your URL must be for example something like ::

rtsp://v1.cache5.c.youtube.com/CjYLENy73wIaLQkUvSkxA_7UKxMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYIPXxZHky7m5Rgw=/0/0/0/video.3gp

(try with this URL)

using the rtsp protocol and a .3gp video file with the correct codecs supported for android .

相思碎 2024-09-22 16:20:21

我改编了 Mark 的答案以使用最新的 Apache HttpComponents:

import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
// ...
    private static String resolveRedirect(String url) throws IOException {
        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();

        CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

        HttpGet httpget = new HttpGet(url);
        HttpContext context = new BasicHttpContext();

        HttpResponse response = httpClient.execute(httpget, context);

        // If we didn't get a '302 Found' we aren't being redirected.
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY)
        {
            throw new IOException(response.getStatusLine().toString());
        }

        Header loc[] = response.getHeaders("Location");
        return loc.length > 0 ? loc[0].getValue() : null;
    }
// ...

I adapted Mark's answer to use up-to-date Apache HttpComponents:

import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
// ...
    private static String resolveRedirect(String url) throws IOException {
        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();

        CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

        HttpGet httpget = new HttpGet(url);
        HttpContext context = new BasicHttpContext();

        HttpResponse response = httpClient.execute(httpget, context);

        // If we didn't get a '302 Found' we aren't being redirected.
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY)
        {
            throw new IOException(response.getStatusLine().toString());
        }

        Header loc[] = response.getHeaders("Location");
        return loc.length > 0 ? loc[0].getValue() : null;
    }
// ...
沒落の蓅哖 2024-09-22 16:20:21

这是一个简单的例子

public class StreamVideo extends Activity
{
VideoView video;
@Override
protected void onCreate(Bundle savedInstanceState)  
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.streamvideo);
    video = (VideoView)findViewById(R.id.videoView1);
    MediaController mc= new MediaController(this);
    mc.setAnchorView(video);
    mc.setMediaPlayer(video);
    video.setMediaController(mc);  
    try
    {       
        Uri uri = Uri.parse("rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov");
        video.setVideoURI(uri);
        video.start();
    }catch (Exception e)
    {
        Log.v("Video playing", e.getMessage());
    }
}   
}

Here is a simple example

public class StreamVideo extends Activity
{
VideoView video;
@Override
protected void onCreate(Bundle savedInstanceState)  
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.streamvideo);
    video = (VideoView)findViewById(R.id.videoView1);
    MediaController mc= new MediaController(this);
    mc.setAnchorView(video);
    mc.setMediaPlayer(video);
    video.setMediaController(mc);  
    try
    {       
        Uri uri = Uri.parse("rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov");
        video.setVideoURI(uri);
        video.start();
    }catch (Exception e)
    {
        Log.v("Video playing", e.getMessage());
    }
}   
}
书间行客 2024-09-22 16:20:21

php代码:

$out = '#EXTM3U'.PHP_EOL;

$out .= '#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=491520'.PHP_EOL;

$out .= $output_video; //Video's Url.

header('Content-Type:application/octet-stream');

echo $out;

exit;

php code:

$out = '#EXTM3U'.PHP_EOL;

$out .= '#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=491520'.PHP_EOL;

$out .= $output_video; //Video's Url.

header('Content-Type:application/octet-stream');

echo $out;

exit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文