How to play online videos in Android?

前端 未结 3 1065
忘掉有多难
忘掉有多难 2021-01-02 10:20

I am developing a sample media player app in Android to play online videos, I have developed some code to play videos. It plays video from SD card very well, but I am facing

相关标签:
3条回答
  • 2021-01-02 10:39

    Update 2020 Kotlin, AndroidX and Android P +

    ProgressDialog is deprecated so I used Alert Dialog to show a loading dialog while the file is buffering and then start playback:

    private fun playVideo(videopath: String) {
        Log.e("Playing Video File: ", "" + videopath);
        try {
            //Show Loader
            val builder: AlertDialog.Builder = AlertDialog.Builder(this@ScreenCaptureImageActivity);
            builder.setCancelable(false); // if you want user to wait for some process to finish,
            builder.setView(R.layout.layout_loading_dialog);
            progressDialog = builder.create();
    
            //add Controller
            val mediaController = MediaController(this@ScreenCaptureImageActivity);
            videoView.setMediaController(mediaController)
            //Add URI
    
            //Uncomment to play from local path
            //videoView.setVideoURI(Uri.parse(videopath))
    
            //Example Play from Internet
            videoView.setVideoPath("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")
    
            videoView.setOnPreparedListener {
                progressDialog!!.dismiss();
                //Start Playback
                videoView.start()
                Log.e(TAG, "Video Started");
            }
        } catch (e: Exception) {
            progressDialog!!.dismiss();
           Log.e(TAG, "Video Play Error :" + e.localizedMessage);
        }
    }
    

    Loader XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="20dp">
        <ProgressBar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:gravity="center"
            android:text="Please wait! This may take a moment." />
    </LinearLayout>
    

    For Network Access add network config in the manifest, from Android P it's required

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.STORAGE" />
     <application
            ...
            android:networkSecurityConfig="@xml/network_security_config"
            >
    

    Add network_security_config.xml in res/xml

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true">
            <trust-anchors>
                <certificates src="system" />
            </trust-anchors>
        </base-config>
    </network-security-config>
    

    0 讨论(0)
  • 2021-01-02 10:49

    It is very easy to play Live video using Video view in Android. i am pasting code here, try this one it will work fine for you.

    public class PlayVideo extends Activity
    
    {
    
          //private String videoPath ="Url";
    
          private static ProgressDialog progressDialog;
          String videourl; 
          VideoView videoView ; 
    
          protected void onCreate(Bundle savedInstanceState)
          {
    
                super.onCreate(savedInstanceState);
                setContentView(R.layout.play_video);
    
                videoView = (VideoView) findViewById(R.id.videoView);
    
                progressDialog = ProgressDialog.show(PlayVideo.this, "", "Buffering                 
                 video...",true);
                progressDialog.setCancelable(true); 
    
    
                PlayVideo();
    
          }
          private void PlayVideo()
          {
                try
                {    
                      getWindow().setFormat(PixelFormat.TRANSLUCENT);
                      MediaController mediaController = new MediaController(PlayVideo.this);
                      mediaController.setAnchorView(videoView);         
    
                      Uri video = Uri.parse(videourl);           
                      videoView.setMediaController(mediaController);
                      videoView.setVideoURI(videoPath);
                      videoView.requestFocus();            
                      videoView.setOnPreparedListener(new OnPreparedListener()
                      {
    
                            public void onPrepared(MediaPlayer mp)
                            {                 
                                  progressDialog.dismiss();   
                                  videoView.start();
                            }
                      }); 
    
                }
                catch(Exception e)
                {
                      progressDialog.dismiss();
                      System.out.println("Video Play Error :"+e.toString());
                      finish();
                } 
    
          }
    }
    

    You can find code from below link:

    http://hasmukhbhadani.blogspot.in/search/label/Video-Play%20Live%20Streaming%20in%20Android.

    0 讨论(0)
  • 2021-01-02 10:53

    try like this

     VideoView v;
     MediaController mediaController;
     ProgressDialog progressDialog;
    

    then

    public void playvideo(String videopath) {
        Log.e("entered", "playvide");
        Log.e("path is", "" + videopath);
        try {
            progressDialog = ProgressDialog.show(VideoPlay.this, "",
                    "Buffering video...", false);
            progressDialog.setCancelable(true);
            getWindow().setFormat(PixelFormat.TRANSLUCENT);
    
            mediaController = new MediaController(VideoPlay.this);
    
            Uri video = Uri.parse(videopath);
            v.setMediaController(mediaController);
            v.setVideoURI(video);
    
            v.setOnPreparedListener(new OnPreparedListener() {
    
                public void onPrepared(MediaPlayer mp) {
                    progressDialog.dismiss();
                    v.start();
                }
            });
    
        } catch (Exception e) {
            progressDialog.dismiss();
            System.out.println("Video Play Error :" + e.getMessage());
        }
    
    }
    

    if still has problem "this video cannot be played" try to change video format , hope it will help u.

    0 讨论(0)
提交回复
热议问题