开发者

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

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

            }

     });  
 }  


Try this:

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.parse(videoPath), "video/mp4");
startActivity(intent);


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜