无法使用 exoplayer 解析方法 getUserAgent()

Cannot resolve method getUserAgent() with exoplayer

我该如何解决

cannot resolve method getUserAgent

我试图用 exoplayer 创建一个视频播放器,但我收到错误 String playerInfo = Util.getUserAgent(this, "Video Player");

这里是完整代码

package com.sanoj.myapplication.Activities;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.bumptech.glide.util.Util;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.sanoj.myapplication.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VideoPlayerActivity extends AppCompatActivity {

    Uri videoUri;
    @BindView(R.id.playerView) PlayerView playerView;

    ExoPlayer exoPlayer;
    ExtractorsFactory extractorsFactory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        ButterKnife.bind(this);
        Intent intent = getIntent();

        if (intent!=null){
            String uri_str = intent.getStringExtra("videoUri");
            videoUri = Uri.parse(uri_str);
        }

        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

        TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
        exoPlayer = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
        extractorsFactory = new DefaultExtractorsFactory();
        playVideo();

    }

    private void playVideo() {
        try{
            String playerInfo = Util.getUserAgent(this, "Video Player"); //error here
            DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,playerInfo);

            MediaSource mediaSource = new ExtractorMediaSource(videoUri,dataSourceFactory,extractorsFactory,null,null);

            playerView.setPlayer(exoPlayer);
            exoPlayer.prepare(mediaSource);
            exoPlayer.setPlayWhenReady(true);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

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

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exoPlayer.setPlayWhenReady(false);

    }
}

为什么会出现这个错误,或者有什么办法可以解决这个错误? 还是我配置有误?任何解决这个问题的想法。

我的gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.sanoj.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


    implementation 'com.google.android.exoplayer:exoplayer:2.7.3'
    implementation 'com.android.support:design:28.0.0'

    implementation 'com.github.bumptech.glide:glide:3.7.0'

    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.jakewharton:butterknife:8.5.1'
    implementation 'com.jakewharton:butterknife-compiler:8.5.1'
    implementation 'commons-io:commons-io:2.6'
}

正确导入 Util.

public static String getUserAgent(Context context,
                                  String applicationName)

Returns a user agent string based on the given application name and the library version.

不要

import com.bumptech.glide.util.Util;

import com.google.android.exoplayer2.util.Util

您正在导入错误的 Util class 文件。您需要删除此行。

import com.bumptech.glide.util.Util; 

并添加此行以调用 exoplayer 正确的 util class.

import com.google.android.exoplayer2.util.Util;