繁体   English   中英

有没有在Android中加载视频的解决方案? 我尝试加载但给消息无法播放视频

[英]Is There Have Solution To Load Video In Android? I Try To Load But Give Message Can't Play Video

我有以下错误:

无法播放此视频。

如下图所示:

在此处输入图片说明

登录AppActivity.java:

 protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_app);

    emailInput = (EditText) findViewById(R.id.Email);
    passwordInput = (EditText) findViewById(R.id.Password);
    btnLogin = (Button) findViewById(R.id.buttonlogin);
    videoview = (VideoView) findViewById(R.id.bgVideo);
    String videoPath = "android:resource//"+getPackageName()+"/"+R.raw.bg3;
    String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
    Uri vidUri = Uri.parse(vidAddress);
    Uri uri = Uri.parse(videoPath);
    videoview.setVideoURI(uri);
    videoview.requestFocus();
    videoview.start();
    videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setLooping(true);
        }
    });

这对于活动登录应用程序 xml activity_login_app.xml:

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <VideoView
        android:id="@+id/bgVideo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center"
        />

</RelativeLayout>

是的,我同意在 VdeoView 中我们无法看到所有视频,并且出现错误

无法播放此视频

所以,我有其他方法。 这些是加载视频的步骤:

第1步

在应用级build.gradle添加此依赖build.gradle

// Exo-Player
implementation 'com.google.android.exoplayer:exoplayer:2.15.0'

第2步

将此视图添加到您的活动/片段.xml文件中:

<com.google.android.exoplayer2.ui.PlayerView
    android:id="@+id/playerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#A6000000" />

第 3 步

将此代码添加到您的.java文件中以加载/显示视频:

// Declare variables
private PlayerView playerView;
private SimpleExoPlayer player;

// Initialize variable
playerView = findViewById(R.id.playerView);
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
            new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
            new DefaultTrackSelector(videoTrackSelectionFactory);

 player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

// Set player and play video
playerView.setPlayer(player);
MediaSource mVideoSource = buildMediaSource(Uri.parse(videoUri)); // videoUri is URL.
player.prepare(mVideoSource); // set ready
player.setPlayWhenReady(true); // set true to play when our player is reay to use.

在同一文件中添加以下方法:

private MediaSource buildMediaSource(Uri mUri) {
    // Measures bandwidth during playback. Can be null if not required.
    DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    // Produces DataSource instances through which media data is loaded.
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
            Util.getUserAgent(this, getString(R.string.app_name)), bandwidthMeter);
    // This is the MediaSource representing the media to be played.
    return new ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(uri);
}

我希望它会像魅力一样发挥作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM