Play video from url in VideoView [Android]

2019-01-28 02:01发布

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>

3条回答
趁早两清
2楼-- · 2019-01-28 02:26

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();
    }
}
查看更多
仙女界的扛把子
3楼-- · 2019-01-28 02:37

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

查看更多
萌系小妹纸
4楼-- · 2019-01-28 02:44

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

    final VideoView videoView;
    videoView = (VideoView)findViewById(R.id.videoView);
    videoView.setVideoPath("http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4");
    videoView.start();
查看更多
登录 后发表回答