Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaPlayer Streaming from a PHP Redirect does not work-out!

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!

like image 365
Guilherme Avatar asked Aug 25 '10 21:08

Guilherme


2 Answers

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;
}
like image 77
Mark G. Avatar answered Oct 20 '22 13:10

Mark G.


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 .

like image 25
Jorgesys Avatar answered Oct 20 '22 14:10

Jorgesys