首页 > 解决方案 > 如何获得时间在 Android 中加载视频

问题描述

我在 Android 中有一个简单的视频播放器,可以播放给定 URL 的视频。我试图在下面的 textView 中显示开始播放视频所需的时间(buffering_textView)。我所做的是花时间到达OnPreparedListener(),但这并不准确(它显示实际视频开始播放之前的时间)。

这样做的正确方法是什么?基本上对于较大的文件,加载时间应该更长。

主要活动:

    package com.example.videoplayer;
    
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.webkit.URLUtil;
    import android.widget.MediaController;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.VideoView;
    
    public class MainActivity extends AppCompatActivity {
    private static final String VIDEO_SAMPLE =
            "https://dl5.webmfiles.org/video-h264.mkv";
    private VideoView vw1, vw2;
    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_main);

        vw1 = findViewById(R.id.videoView1);
        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 MyMediaController(vw1.getContext());
        controller.setMediaPlayer(vw1);
        vw1.setMediaController(controller);
    }


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

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

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

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

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

    private void initializePlayer() {
        // Show the "Buffering..." message while the video loads.
        mBufferingTextView.setVisibility(VideoView.VISIBLE);
        // Buffer and decode the video sample.
        Uri videoUri = Uri.parse(VIDEO_SAMPLE);
        vw1.setVideoURI(videoUri);

        long t0 = System.currentTimeMillis();
        // Listener for onPrepared() event (runs after the media is prepared).
        vw1.setOnPreparedListener(
                new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        // Hide buffering message.

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

                        // Start playing!
                        vw1.start();
                        long t1 = System.currentTimeMillis();
                        mBufferingTextView.setText((t1-t0)+", "+mediaPlayer.getDuration()+" "+ mediaPlayer.isPlaying());
                    }
                });

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

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


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

}

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <!-- adding VideoView to the layout -->
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/buffering_textview"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="Buffering..."
        />

</LinearLayout>

标签: javaandroidvideo-streamingandroid-mediaplayerandroid-videoview

解决方案


推荐阅读