我如何解决
无法解决的方法
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)
根据给定的应用程序名称和库版本返回一个用户代理字符串。
不要
import com.bumptech.glide.util.Util;
做
import com.google.android.exoplayer2.util.Util
你导入了错误的Util类文件。你需要删除这一行。
import com.bumptech.glide.util.Util;
并添加这一行来调用exoplayer正确的Util类。
import com.google.android.exoplayer2.util.Util;