Android Help: How do open a remote Video file URL to play in MediaPlayer without having to open a browser window?
Asked Answered
P

4

14

How do open a remote Video file URL from a button click to play in the internal MediaPlayer without having to open a browser window?

The video plays fine, but it always opens a browser window 1st which is annoying.

This is what i am using already, but is it possible to launch the mediaplayer without the app opening a browser window first.

Hope someone can help

Thanks Lucy

final Button button = (Button) findViewById(R.id.play);  
     button.setOnClickListener(new Button.OnClickListener() {  
         public void onClick(View v) {  
             // Perform action on click 
             Uri uri = Uri.parse("http://domain.com/videofile.mp4");
             Intent intent = new Intent(Intent.ACTION_VIEW, uri);

             startActivity(intent);

            }

     });  
 }  
Pernell answered 6/1, 2011 at 20:24 Comment(0)
S
26

Try this:

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.parse(videoPath), "video/mp4");
startActivity(intent);
Scouring answered 2/3, 2012 at 10:39 Comment(1)
For some reason splitting the setDataAndType() into 2 lines doesn't work for me: intent.SetData(Uri.parse(videoPath)); intent.SetType("video/mp4"); ...not a huge deal, but thought I'd mention it in case someone else has the same issue.Regal
L
11

Try adding the MIME type to the Intent. Right now, you are routing to the browser, which does an HTTP HEAD, determines the MIME type, then routes it to the proper app. If you put the MIME type in yourself, that should skip the browser step.

Lionize answered 6/1, 2011 at 21:18 Comment(4)
Thankyou, please could you show me how to code it into the above, i'm not quite sure how to do that, thankyou ever so much, LucyPernell
@lucy: Intent intent=new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "..."); where ... is the MP4 MIME type (video/mp4, maybe)Lionize
@Lionize I used this way : Intent intent=new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("javmed-prod.s3.amazonaws.com/…), "video/mp4"); startActivity(intent);..and got ActivityNotFound..also tried "video/*"...any idea Sir ?Kiowa
@Lionize okay i got my ans from your another post in SO #1426002 link is not supporting the Streaming ..i will need to first download and then play..thank you sir.Kiowa
H
5

You need to set the videoUrl and mime type (video/mp4) on the intent, i.e.:

String videoUrl = "http://videosite/myvideo.mp4";
Intent playVideo = new Intent(Intent.ACTION_VIEW); 
playVideo.setDataAndType(Uri.parse(videoUrl), "video/mp4");
startActivity(playVideo);
Hoberthobey answered 18/7, 2015 at 10:51 Comment(1)
@Brad Playing video like this is working for me. But i have a case that i want to add an additional headers to the HTTP query. I add viewIntent.putExtra(Browser.EXTRA_HEADERS, bundle), but only adds a header when using setData() instead of setDataAndType(). My idea is that i want to play video but for that the server needs to authorize me using headers in the http query...Arv
R
1

Based on this answer I would suggest to have an activity chooser, where video can be open either as a Uri(browser) or a video(video player). Just in case the default video player is not very good for streaming like in some Huawei devices.

This way the user has the option to open it in a browser where he can even download it if it is chrome

The solution would look like this:

    val uri = Uri.parse("your url")

    val generalViewIntent = Intent(Intent.ACTION_VIEW)
    generalViewIntent.data = uri
    val intentVideo = Intent(Intent.ACTION_VIEW)
    intentVideo.setDataAndType(uri, "video/mp4")

    val chooserIntent = Intent.createChooser(
        intentVideo,
        "Choose application to open video"
    )
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(generalViewIntent))
    try {
        startActivity(chooserIntent)
    } catch (e: Exception) {
        startActivity(generalViewIntent)
    }

First the app will check if it can open both as video and as url, if no video is found then it will throw exception. In case it throws it has to try again with only browser.

Based on your requirement you can add intent flags.

Ranit answered 20/5, 2022 at 16:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.