簡體   English   中英

Android 幫助:如何打開遠程視頻文件 URL 以在 MediaPlayer 中播放而無需打開瀏覽器窗口?

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

如何通過單擊按鈕打開遠程視頻文件 URL 以在內部 MediaPlayer 中播放,而無需打開瀏覽器窗口?

視頻播放正常,但它總是第一個打開瀏覽器窗口,這很煩人。

這是我已經在使用的,但是是否可以在應用程序先打開瀏覽器窗口的情況下啟動媒體播放器。

希望有人可以幫助

謝謝露西

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

            }

     });  
 }  

嘗試這個:

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

嘗試將 MIME 類型添加到Intent 現在,您正在路由到瀏覽器,它執行HTTP HEAD ,確定 MIME 類型,然后將其路由到正確的應用程序。 如果您自己輸入 MIME 類型,則應該跳過瀏覽器步驟。

您需要在意圖上設置videoUrl和 mime 類型( video/mp4 ),即:

String videoUrl = "http://videosite/myvideo.mp4";
Intent playVideo = new Intent(Intent.ACTION_VIEW); 
playVideo.setDataAndType(Uri.parse(videoUrl), "video/mp4");
startActivity(playVideo);

基於這個答案,我建議有一個活動選擇器,其中視頻可以作為 Uri(瀏覽器)或視頻(視頻播放器)打開。 以防萬一默認視頻播放器不適用於某些華為設備中的流媒體播放。

這樣,用戶可以選擇在瀏覽器中打開它,如果它是chrome ,他甚至可以下載它

解決方案如下所示:

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

首先,應用程序將檢查它是否可以作為視頻和 url 打開,如果沒有找到視頻,則會拋出異常。 如果它拋出它必須只用瀏覽器重試。

根據您的要求,您可以添加意圖標志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM