来自PHP重定向的Android MediaPlayer流媒体无法运行!
我工作的那家公司正在开发一款Android应用程序,它可以从web上的URL播放视频文件。这个 视频URL是PHP脚本的一个参数,可对其进行正确编码并重定向到编码视频,如下所示:来自PHP重定向的Android MediaPlayer流媒体无法运行!,php,android,video,redirect,media-player,Php,Android,Video,Redirect,Media Player,我工作的那家公司正在开发一款Android应用程序,它可以从web上的URL播放视频文件。这个 视频URL是PHP脚本的一个参数,可对其进行正确编码并重定向到编码视频,如下所示: header('Content-Type: video/'.$format); header('Location:'.$output_video); 其中,$output\u video是编码视频的URL(如果我们在浏览器中使用此URL,它会工作),而$format是视频格式 但是,当我尝试使用流模式从API演示中执
header('Content-Type: video/'.$format);
header('Location:'.$output_video);
其中,$output\u video
是编码视频的URL(如果我们在浏览器中使用此URL,它会工作),而$format
是视频格式
但是,当我尝试使用流模式从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)
有人知道如何解决这个问题吗
提前谢谢 响应是您试图在MediaPlayer中传输的文件,您的URL必须是,例如:
rtsp://v1.cache5.c.youtube.com/CjYLENy73wIaLQkUvSkxA_7UKxMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYIPXxZHky7m5Rgw=/0/0/0/video.3gp
(请尝试使用此URL)
使用rtsp协议和.3gp视频文件时,我遇到了同样的问题。android MediaPlayer无法处理重定向,因此您必须找到php脚本将您重定向到的位置,并按照Jeorgesys的解释为其提供rtsp URL 我能够通过执行HttpGet而不遵循任何重定向来解决这个问题,然后从“Location”Http头中提取rtspurl。如果你有多个重定向,你会有一点麻烦,但幸运的是在我的情况下,我只需要担心一个重定向
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;
}
下面是一个简单的例子
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());
}
}
}
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;
我修改了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;
}
// ...