在VideoView中从URL播放视频[Android]

dba5bblo  于 2023-09-28  发布在  Android
关注(0)|答案(7)|浏览(166)

我发现了类似的问题,但对我没有任何作用。我试着从这个URL播放视频:

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

我的java代码:

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

当我运行应用程序时,Activity中没有显示任何内容,IDE也没有显示任何错误。有什么想法吗?
编辑:
我想在其中显示视频的活动:

<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>
lqfhib0f

lqfhib0f1#

试试这个代码。这段代码对我来说非常好用。

VideoView videoView = findViewById(R.id.videoView);
videoView.setVideoPath("http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4");
videoView.start();
41ik7eoe

41ik7eoe2#

对我来说,把URL从

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

收件人:

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

成功了
换句话说,我使用HTTPS而不是HTTP。
我使用以下代码来启动视频:

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

n6lpvg4x3#

我知道你不应该用wrap_content来表示VideoView的高度。VideoView在视频缓存后未自行调整大小

snvhrwxg

snvhrwxg4#

请添加互联网权限,将layout_height wrap_content更改为match parent。这是这个问题的代码

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();
    }
}
5rgfhyps

5rgfhyps5#

videoView.setURI(Uri.parse(url));// use methods to set url
videoView.start();

然后接受许可

<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"/>
5gfr0r5j

5gfr0r5j6#

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

        }
    }

}
m2xkgtsf

m2xkgtsf7#

首先,您需要在AndroidManifest.xml文件中设置Internet权限,如下所示:

<uses-permission android:name="android.permission.INTERNET" />

然后在Android中播放来自URL的视频,使用下面的结构以获得更多的可读性,我评论了代码:)

// Your Video URL
    String videoUrl = "https://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4";

    // on below line we are initializing our variables.
    videoView = findViewById(R.id.videoView);

    // Uri object to refer the
    // resource from the videoUrl
    Uri uri = Uri.parse(videoUrl);

    // sets the resource from the
    // videoUrl to the videoView
    videoView.setVideoURI(uri);

    // creating object of
    // media controller class
    MediaController mediaController = new MediaController(this);

    // sets the anchor view
    // anchor view for the videoView
    mediaController.setAnchorView(videoView);

    // sets the media player to the videoView
    mediaController.setMediaPlayer(videoView);

    // sets the media controller to the videoView
    videoView.setMediaController(mediaController);

    // starts the video
    videoView.start();

相关问题