Android Exoplayer only playing audio without video
Asked Answered
R

9

9

I'm accessing an mp4 file on the web - it runs just fine in the browser but for some reason I can only seem to get the audio portion to work in my app. Any help would be appreciated.

Relevant 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"
}

Layout View:

<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"
     />

Relevant code within Fragment:

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);
Resection answered 31/1, 2019 at 0:10 Comment(0)
R
3

Oh boy is this a weird one... so it turns out that the issue is that setting the background theme for the app causes the exoplayer display to be hidden...

So even just having this defined was enough. If I removed the background from here, everything works. I still don't know how to both show this video and have a background set, though...

> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
>     <!-- Customize your theme here. -->
>     <item name="android:background">@color/colorPrimary</item> </style>

I hope that having this here will help someone else in the future avoid this issue.

Resection answered 1/2, 2019 at 5:43 Comment(2)
Are you sure this was the problem? Neither HlsMediaSource or this solution worked for me :/ Still playing the audio but video screen is blank.Beaut
never mind :) My problem was usng: new ExoPlayer.Builder(this, new MediaCodecVideoRenderer(this, MediaCodecSelector.DEFAULT), new MediaCodecAudioRenderer(this, MediaCodecSelector.DEFAULT)).build(); I have changed it to: new SimpleExoPlayer.Builder(this).build();Beaut
K
3

In my case, hardware-accelerated was set to false in the manifest.

android:hardwareAccelerated="true" is needed for texture view in exoplayer to work.

Kino answered 9/10, 2019 at 12:13 Comment(0)
K
2

I've had the same problem. I forgot to add the code below, it was fixed when I added it.

playerView.setPlayer(player);
Kremer answered 6/11, 2020 at 12:35 Comment(0)
F
1

Would you mind trying HlsMediaSource? This normally works for me

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)

Also, adding this to your gradle's dependencies

implementation "com.google.android.exoplayer:extension-okhttp:2.7.2"
Feints answered 31/1, 2019 at 2:53 Comment(3)
what dependencies do you use for this?Resection
Now it just plays nothingResection
Thanks. This turned out to be a pretty weird one. Check out my answer below.Resection
H
1

Please check your code , PlayerView setPlayer more than one may lead to the problem ...

Hippogriff answered 27/9, 2020 at 11:47 Comment(0)
B
0

My case was weird. I tried to avoid deprecated methods and i was using:

new ExoPlayer.Builder(this, new MediaCodecVideoRenderer(this, MediaCodecSelector.DEFAULT), new MediaCodecAudioRenderer(this, MediaCodecSelector.DEFAULT)).build();

I have changed it to:

new SimpleExoPlayer.Builder(this).build();

This solved my problem.

Beaut answered 22/3, 2020 at 10:18 Comment(0)
G
0

in my case I had this style

 <style name="Theme.EnglishApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="colorPrimary">@color/red700</item>
    <item name="colorPrimaryDark">@color/red900</item>
    <item name="colorAccent">@color/red700</item>
    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    <!-- the problem is this item -->
    <item name="android:background">@color/black</item>
    <!-- the problem is this item -->
</style>

and when I removed this item

<item name="android:background">@color/black</item>

my problem fixed

Gryphon answered 23/3, 2022 at 21:42 Comment(0)
T
0

The issue was that the theme used in the manifest had background defined as

<item name="android:background">@color/white</item>

which was making the video kind of invisible and instead showing a white screen. Removing this line solved the problem

Tann answered 19/4, 2022 at 1:48 Comment(0)
L
0

In my case, I had set android:layerType="hardware". I was testing out how I can improve my recycler view performance. That was the problem. removed it from everywhere and I got back my visuals.

Lourielouse answered 10/1, 2024 at 8:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.