Android MediaPlayer 从 PHP 重定向进行流媒体播放不成功!

7

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

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

其中$output_video是编码视频的URL(如果我们在浏览器中使用此URL,则可以正常工作),而$format是视频格式。

但是,当我尝试使用流媒体模式执行API演示中的MediaPlayerDemo_Video时,会出现如下错误:

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)

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

提前感谢!

5个回答

9

我遇到了同样的问题。 原来安卓MediaPlayer无法处理重定向,因此您必须找到php脚本将您重定向的位置,并按Jeorgesys所解释的方式提供rtsp URL。

我通过执行HttpGet并不跟随任何重定向来解决问题,然后从“Location” 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;
}

2

响应是您正在尝试在MediaPlayer中流式传输的文件,您的URL必须像这样:

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

(请使用此URL进行尝试)

使用rtsp协议和正确支持Android的编解码器.3gp视频文件。


0

我改编了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;
    }
// ...

-1

这里是一个简单的例子

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());
    }
}   
}

-1

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接