我正在访问Web上的mp4文件-该文件在浏览器中可以正常运行,但是由于某些原因,我似乎只能在应用中使用音频部分。任何帮助将不胜感激。
相关Gradle:
android {
compileSdkVersion 28
minSdkVersion 17
targetSdkVersion 28
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation "com.android.support:support-compat:28.0.0"
implementation "com.android.support:support-media-compat:28.0.0"
implementation "com.google.android.exoplayer:exoplayer-core:2.7.2"
implementation "com.google.android.exoplayer:exoplayer-ui:2.7.2"
}
布局视图:
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/playerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@+id/horizontalHalf"
/>
片段中的相关代码:
private SimpleExoPlayer mExoPlayer;
private static MediaSessionCompat mMediaSession;
private PlaybackStateCompat.Builder mStateBuilder;
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
DefaultRenderersFactory renderersFactory = new DefaultRenderersFactory(mParentActivity);
mExoPlayer = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector, loadControl);
mPlayerView.setPlayer(mExoPlayer);
mExoPlayer.addListener(this);
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(mParentActivity, "APP_NAME");
DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).setExtractorsFactory(extractorsFactory).createMediaSource(mediaUri);
mExoPlayer.prepare(mediaSource);
mExoPlayer.setPlayWhenReady(true);
答案 0 :(得分:1)
您介意尝试HlsMediaSource
吗?这通常对我有用
val bandwidthMeter = DefaultBandwidthMeter()
val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(bandwidthMeter)
val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
player = ExoPlayerFactory.newSimpleInstance(context, trackSelector)
val dataSourceFactory = OkHttpDataSourceFactory(
OkHttpClient(),
Util.getUserAgent(context, context.getString(R.string.app_name)),
bandwidthMeter
)
val videoSource = HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(url))
player?.prepare(videoSource)
此外,将其添加到您的gradle
的依赖关系中
implementation "com.google.android.exoplayer:extension-okhttp:2.7.2"
答案 1 :(得分:0)
天哪,这是一个奇怪的家伙...所以事实证明,问题在于为应用设置背景主题会导致exoplayer的显示被隐藏...
因此,只需定义此定义就足够了。如果我从此处删除背景,则一切正常。我仍然不知道如何同时显示该视频 和背景...
> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
> <!-- Customize your theme here. -->
> <item name="android:background">@color/colorPrimary</item> </style>
我希望在这里拥有此功能将来可以帮助其他人避免此问题。
答案 2 :(得分:0)
在我的情况下,清单中的硬件加速设置为false。
android:hardwareAccelerated="true"
是exoplayer中的纹理视图正常工作所必需的。
答案 3 :(得分:0)
我的案子很奇怪。我试图避免使用过时的方法,而我使用的是:
new ExoPlayer.Builder(this, new MediaCodecVideoRenderer(this, MediaCodecSelector.DEFAULT), new MediaCodecAudioRenderer(this, MediaCodecSelector.DEFAULT)).build();
我将其更改为:
new SimpleExoPlayer.Builder(this).build();
这解决了我的问题。
答案 4 :(得分:0)
请检查您的代码,PlayerView setPlayer不止一个可能导致问题...