27

I found a similar questions but nothing work for me. I try play video from this url:

http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4

My java code:

VideoView videoView= (VideoView)findViewById(R.id.exerciseVideo);
    Uri uri = Uri.parse(TEST_URL);
    videoView.setVideoURI(uri);
    videoView.requestFocus();
    videoView.start();

When I run app nothing is displayed in activity and IDE does not show any errors. ANy idea, please?

EDIT:

My activity where I want to show video:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.martin.fitnessapp.ExerciseDetailActivity"
        android:orientation="vertical">


        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="2">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/exerciseImgA"
                android:layout_weight="1"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:paddingRight="8dp"/>

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/exerciseImgB"
                android:layout_weight="1"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:paddingLeft="8dp"/>
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text=""
            android:id="@+id/exerciseDesc" />

        <VideoView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/exerciseVideo" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/guideImg"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"/>


    </LinearLayout>
</ScrollView>
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
Aligator
  • 737
  • 2
  • 10
  • 23
  • Did you read logcat to see if it has any explanation? Did you confirm that the device can even go to that url? Are you sure its not session protected and non-public? – JoxTraex Nov 04 '16 at 23:52
  • 1
    I've tried another one online and one video from local ... no one works – Aligator Nov 04 '16 at 23:59
  • confirm by going that url on your browser ON THE DEVICE to confirm what the behavior is. You need to see if its even a valid URL that you can access in a previously un-authed session. Don't assume just because it load on the web that it'll load for you, sessions make a big difference. – JoxTraex Nov 05 '16 at 00:05
  • in browser works fine. – Aligator Nov 05 '16 at 00:20

6 Answers6

47

Try this code.. This code works perfectly for me..

VideoView videoView = findViewById(R.id.videoView);
videoView.setVideoPath("http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4");
videoView.start();
Jameson
  • 6,400
  • 6
  • 32
  • 53
Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34
8

For me, changing the URL from

"http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4"

to:

"https://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4"

made it work.

In other words, I used HTTPS instead of HTTP.

I used the following code to start the video:

final VideoView videoView = findViewById(R.id.videoview); //id in your xml file
videoView.setVideoURI(Uri.parse(URL)); //the string of the URL mentioned above
videoView.requestFocus();
videoView.start();
mickmick
  • 81
  • 1
  • 2
4

As I know you shouldn't use wrap_content for VideoView height. VideoView didn't resize itself after video cached

hluhovskyi
  • 9,556
  • 5
  • 30
  • 42
3

Kindly add Internet permission them Change layout_height wrap_content into match parent. this is a code for this problem

public class MainActivity extends Activity {
    private ProgressDialog bar;
    private String path="https://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4";
    private MediaController ctlr;
    private VideoView videoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.activity_main);
      bar=new ProgressDialog(MainActivity.this);
        bar.setTitle("Connecting server");
        bar.setMessage("Please Wait... ");
        bar.setCancelable(false);
        bar.show();
       if(bar.isShowing()) {
           videoView = findViewById(R.id.v1);
           Uri uri = Uri.parse(path);
           videoView.setVideoURI(uri);
           videoView.start();
           ctlr = new MediaController(this);
           ctlr.setMediaPlayer(videoView);
           videoView.setMediaController(ctlr);
           videoView.requestFocus();
       }
        bar.dismiss();
    }
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
2
videoView.setURI(Uri.parse(url));// use methods to set url
videoView.start();

then accept permission

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
0
public class ShowVideoActivity extends AppCompatActivity {
    private static final String VIDEO_SAMPLE =
            "https://www.youtube.com/watch?v=HexFqifusOk&list=RDHexFqifusOk&start_radio=1";
    private VideoView mVideoView;
    private TextView mBufferingTextView;
    // Current playback position (in milliseconds).
    private int mCurrentPosition = 0;
    // Tag for the instance state bundle.
    private static final String PLAYBACK_TIME = "play_time";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_video);

        mVideoView = findViewById(R.id.videoview);
        mBufferingTextView = findViewById(R.id.buffering_textview);

        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(PLAYBACK_TIME);
        }

        // Set up the media controller widget and attach it to the video view.
        MediaController controller = new MediaController(this);
        controller.setMediaPlayer(mVideoView);
        mVideoView.setMediaController(controller);
    }


    @Override
    protected void onStart() {
        super.onStart();

        // Load the media each time onStart() is called.
        initializePlayer();
    }

    @Override
    protected void onPause() {
        super.onPause();

        // In Android versions less than N (7.0, API 24), onPause() is the
        // end of the visual lifecycle of the app.  Pausing the video here
        // prevents the sound from continuing to play even after the app
        // disappears.
        //
        // This is not a problem for more recent versions of Android because
        // onStop() is now the end of the visual lifecycle, and that is where
        // most of the app teardown should take place.
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            mVideoView.pause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

        // Media playback takes a lot of resources, so everything should be
        // stopped and released at this time.
        releasePlayer();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current playback position (in milliseconds) to the
        // instance state bundle.
        outState.putInt(PLAYBACK_TIME, mVideoView.getCurrentPosition());
    }

    private void initializePlayer() {
        // Show the "Buffering..." message while the video loads.
        mBufferingTextView.setVisibility(VideoView.VISIBLE);

        // Buffer and decode the video sample.
        Uri videoUri = getMedia(VIDEO_SAMPLE);
        mVideoView.setVideoURI(videoUri);

        // Listener for onPrepared() event (runs after the media is prepared).
        mVideoView.setOnPreparedListener(
                new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {

                        // Hide buffering message.
                        mBufferingTextView.setVisibility(VideoView.INVISIBLE);

                        // Restore saved position, if available.
                        if (mCurrentPosition > 0) {
                            mVideoView.seekTo(mCurrentPosition);
                        } else {
                            // Skipping to 1 shows the first frame of the video.
                            mVideoView.seekTo(1);
                        }

                        // Start playing!
                        mVideoView.start();
                    }
                });

        // Listener for onCompletion() event (runs after media has finished
        // playing).
        mVideoView.setOnCompletionListener(
                new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        Toast.makeText(ShowVideoActivity.this,
                                "Completed",
                                Toast.LENGTH_SHORT).show();

                        // Return the video position to the start.
                        mVideoView.seekTo(0);
                    }
                });
    }


    // Release all media-related resources. In a more complicated app this
    // might involve unregistering listeners or releasing audio focus.
    private void releasePlayer() {
        mVideoView.stopPlayback();
    }

    // Get a Uri for the media sample regardless of whether that sample is
    // embedded in the app resources or available on the internet.
    private Uri getMedia(String mediaName) {
        if (URLUtil.isValidUrl(mediaName)) {
            // Media name is an external URL.
            return Uri.parse(mediaName);
        } else {

            // you can also put a video file in raw package and get file from there as shown below

            return Uri.parse("android.resource://" + getPackageName() +
                    "/raw/" + mediaName);


        }
    }

}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 3
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Suraj Rao Mar 16 '21 at 06:09