如何解决ExpoPlayer无法获取网址
我的view
正在显示,但未获取并播放链接。 Internet的权限也已添加到清单文件中。 activity_main
也已正确放置。
package com.example.exoplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
public class MainActivity extends AppCompatActivity {
private PlayerView playerView;
private SimpleExoPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView=findViewById(R.id.playerView);
}
@Override
protected void onStart() {
super.onStart();
player= ExoPlayerFactory.newSimpleInstance(this,new DefaultTrackSelector());
playerView.setPlayer(player);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,Util.getUserAgent(this,getString(R.string.app_name)));
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse("http://buildappswithpaulo.com/videos/outro_music.mp4"));
player.prepare(videoSource);
}
}
解决方法
请按照以下步骤进行交叉检查或复制粘贴
- 然后在清单文件中添加互联网许可以获取视频URL
manifest.xml
<manifest ...>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
//app/build.gradle
apply plugin: 'com.android.application'
android {
...
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
}
dependencies {
...
implementation 'com.google.android.exoplayer:exoplayer:2.10.4'
}
protected void onCreate(Bundle savedInstanceState) {
...
Context ctx =this;
String CONTENT_URL = "http://buildappswithpaulo.com/videos/outro_music.mp4";
int playerID=R.id.pv_main;
int appNameStringRes = R.string.app_name;
startPlayingVideo(this,CONTENT_URL,playerID,appNameStringRes);
}
//
private void startPlayingVideo(Context ctx,String CONTENT_URL,int playerID,String appNameRes) {
PlayerView pvMain = ctx.findViewById(playerID);
//BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
//TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
//TrackSelector trackSelectorDef = new DefaultTrackSelector(videoTrackSelectionFactory);
TrackSelector trackSelectorDef = new DefaultTrackSelector();
SimpleExoPlayer absPlayerInternal = ExoPlayerFactory.newSimpleInstance(ctx,trackSelectorDef);
String userAgent = Util.getUserAgent(ctx,ctx.getString(appNameRes));
DefaultDataSourceFactory defdataSourceFactory = new DefaultDataSourceFactory(ctx,userAgent);
Uri uriOfContentUrl = Uri.parse(CONTENT_URL);
MediaSource mediaSource = new ProgressiveMediaSource.Factory(defdataSourceFactory).createMediaSource(uriOfContentUrl);
absPlayerInternal.prepare(mediaSource);
absPlayerInternal.setPlayWhenReady(true);
pvMain.setPlayer(absPlayerInternal);
}
private void stopPlayer(PlayerView pv,SimpleExoPlayer absPlayer){
pv.setPlayer(null);
absPlayer.release();
absPlayer = null;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。