how to play video from url

前端 未结 9 1810
别那么骄傲
别那么骄傲 2020-11-29 03:56

I am beginner in android development and try to play video from link. But it\'s giving error \"sorry,we can\'t play this video\". I tried so many links but for

相关标签:
9条回答
  • 2020-11-29 04:26

    Try this:

    String LINK = "type_here_the_link";
    setContentView(R.layout.mediaplayer);
    VideoView videoView = (VideoView) findViewById(R.id.video);
    MediaController mc = new MediaController(this);
    mc.setAnchorView(videoView);
    mc.setMediaPlayer(videoView);
    Uri video = Uri.parse(LINK);
    videoView.setMediaController(mc);
    videoView.setVideoURI(video);
    videoView.start();
    
    0 讨论(0)
  • 2020-11-29 04:31
    pDialog = new ProgressDialog(this);
    
        // Set progressbar message
        pDialog.setMessage("Buffering...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        // Show progressbar
        pDialog.show();
    
        try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(this);
            mediacontroller.setAnchorView(mVideoView);      
    
            Uri videoUri = Uri.parse(videoUrl);
            mVideoView.setMediaController(mediacontroller);
            mVideoView.setVideoURI(videoUri);
    
        } catch (Exception e) {
    
            e.printStackTrace();
        }
    
        mVideoView.requestFocus();
        mVideoView.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                mVideoView.start();
            }
        });
        mVideoView.setOnCompletionListener(new OnCompletionListener() {
    
            public void onCompletion(MediaPlayer mp) {
                if (pDialog.isShowing()) {
                    pDialog.dismiss();
                }
                finish();               
            }
        });
    
    0 讨论(0)
  • 2020-11-29 04:31

    I also got stuck with this issue. I got correct response from server, but couldn`t play video. After long time I found a solution here. Maybe, in future this link will be invalid. So, here is my correct code

        Uri video = Uri.parse("Your link should be in this place "); 
        mVideoView.setVideoURI(video); 
    
       mVideoView.setZOrderOnTop(true); //Very important line, add it to Your code
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
            @Override 
            public void onPrepared(MediaPlayer mediaPlayer) {
      // here write another part of code, which provides starting the video
      }}
    
    0 讨论(0)
  • 2020-11-29 04:34

    Try Exoplayer2

    https://github.com/google/ExoPlayer

    It is highly customisable

        private void initializePlayer() {
             player = ExoPlayerFactory.newSimpleInstance(
                 new DefaultRenderersFactory(this),
                 new DefaultTrackSelector(), new DefaultLoadControl());
    
             playerView.setPlayer(player);
    
             player.setPlayWhenReady(playWhenReady);
             player.seekTo(currentWindow, playbackPosition);
    
    Uri uri = Uri.parse(getString(R.string.media_url_mp3));
         MediaSource mediaSource = buildMediaSource(uri);
         player.prepare(mediaSource, true, false);
        }
    
    private MediaSource buildMediaSource(Uri uri) {
      return new ExtractorMediaSource.Factory(
          new DefaultHttpDataSourceFactory("exoplayer-codelab")).
          createMediaSource(uri);
    }
    
    @Override
    public void onStart() {
     super.onStart();
     if (Util.SDK_INT > 23) {
       initializePlayer();
     }
    }
    

    Check this url for more details

    https://codelabs.developers.google.com/codelabs/exoplayer-intro/#2

    0 讨论(0)
  • 2020-11-29 04:35

    Check this UniversalVideoView library its simple and straight forward with controller as well.

    Here is the code to play the video
    Add this dependancyy in build.gradle

     implementation 'com.linsea:universalvideoview:1.1.0@aar'
    

    Java Code

            UniversalVideoView mVideoView = findViewById(R.id.videoView);
            Uri uri=Uri.parse("https://firebasestorage.googleapis.com/v0/b/contactform-d9534.appspot.com/o/Vexento%20-%20Masked%20Heroes.mp4?alt=media&token=74c2e448-5b1b-47b7-b761-66409bcfbf56");
            mVideoView.setVideoURI(uri);
            UniversalMediaController mMediaController = findViewById(R.id.media_controller);
            mVideoView.setMediaController(mMediaController);
            mVideoView.start();   
    

    Xml Code

    <FrameLayout
                android:id="@+id/video_layout"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@android:color/black">
    
                <com.universalvideoview.UniversalVideoView
                    android:id="@+id/videoView"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center"
                    app:uvv_autoRotation="true"
                    app:uvv_fitXY="false" />
    
                <com.universalvideoview.UniversalMediaController
                    android:id="@+id/media_controller"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    app:uvv_scalable="true" />
    
            </FrameLayout>
    
    0 讨论(0)
  • 2020-11-29 04:35

    Check whether your phone supports the video format or not.Even I had the problem when playing a 3gp file but it played a mp4 file perfectly.

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