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

516

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!

348

Answer

Solution:

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;
}
182

Answer

Solution:

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 .

622

Answer

Solution:

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;
    }
// ...
45

Answer

Solution:

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

Answer

Solution:

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;

People are also looking for solutions to the problem: Ouput stuff while php script is running

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.