9

One Strange thing: the code is working fine while using emulator, but crashes while running building an unsigned apk.

when connected to android studio, it gives the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tv2z.demo/com.tv2z.demo.DemoActivity}: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class com.google.android.exoplayer2.ui.SimpleExoPlayerView
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                Caused by: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class com.google.android.exoplayer2.ui.SimpleExoPlayerView
                                                                Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class com.google.android.exoplayer2.ui.SimpleExoPlayerView
                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                   at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                                   at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                   at com.tv2z.demo.DemoActivity.onCreate(***DemoActivity.java:80***)
                                                                   at android.app.Activity.performCreate(Activity.java:6679)
                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                Caused by: java.lang.NoSuchFieldError: No field SimpleExoPlayerView_shutter_background_color of type I in class Lcom/google/android/exoplayer2/ui/R$styleable; or its superclasses (declaration of 'com.google.android.exoplayer2.ui.R$styleable' appears in /data/app/com.tv2z.demo-1/base.apk:classes9.dex)
                                                                   at com.google.android.exoplayer2.ui.SimpleExoPlayerView.<init>(SourceFile:274)
                                                                   at com.google.android.exoplayer2.ui.SimpleExoPlayerView.<init>(SourceFile:233)

This is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DemoActivity">

<TextView
android:id="@+id/sample_app_title"
android:text="ExoPlayer 2 Example App:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000000"
android:gravity="center_horizontal"/>

<TextView
android:id="@+id/resolution_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Resolution"
android:textSize="30px"
android:background="#FFFFFF"
android:textColor="#000000"
android:layout_below="@+id/sample_app_title"
android:gravity="center_horizontal"/>

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/exo_player_view"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/resolution_textView"
android:layout_marginTop="2dp" />
</RelativeLayout>

This is my DemoActivity file: public class DemoActivity extends AppCompatActivity implements View.OnClickListener,PlaybackControlView.VisibilityListener {

private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private static final String TAG = "PlayerActivity";
private SimpleExoPlayer player;
private SimpleExoPlayerView playerView;
String videoURL = "http://www.sample-videos.com/video/mp4/360/big_buck_bunny_360p_30mb.mp4";
private long playbackPosition;
private int currentWindow;
private long playerPosition;
private boolean isTimelineStatic;
private int playerWindow;
private Timeline.Window window;
private boolean shouldAutoPlay;
private CustPlugin plugin;
public Tracker tracker;
public MediaDetails medDetails;
private EventLogger eventLogger;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    shouldAutoPlay = false; // make it false to stop autoplay
    setContentView(R.layout.activity_demo);
    ButterKnife.bind(this);
    window = new Timeline.Window();
    playerView = new SimpleExoPlayerView(this);
    playerView = (SimpleExoPlayerView) findViewById(R.id.exo_player_view);
    playerView.setControllerVisibilityListener(this);
    playerView.requestFocus();
    tracker = ((Tv2zApplication) getApplication()).getTracker();
}

private MediaDetails setMediaDetails() {
    medDetails = new MediaDetails();
    medDetails.setMediaId(CustPlugin.generateUniqueId());
    medDetails.setMediaTitle("Test Exo Android Player");
    medDetails.setMediaUrl(videoURL);
    medDetails.setPlayerName("Exo Android Player");
    medDetails.setWidth("1280");
    medDetails.setHeight("960");
    medDetails.setMediaLength((int)player.getDuration()/1000);
    medDetails.setCdUserId("EXO001");
    medDetails.setCdMediaId("1234");
    medDetails.setCdProgramType("VOD");
    medDetails.setCdProgramId("4567");
    medDetails.setCdDuration(52);
    return medDetails;
}
/**
 // time_to_initial_play will be ZERO, if "shouldAutoPlay" is true
 // The Function onStart() and onResume() are same, as triggered only once when player is loaded
 // Difference is that onStart is triggered for API 23 and up while onResume for API less than 23
 **/
@Override
protected void onStart() {
    super.onStart();
    if (Util.SDK_INT > 23) {
        initializePlayer();
    }
}

@Override
protected void onResume() {
    super.onResume();
    hideSystemUi();
        if (Util.SDK_INT <= 23 || player == null) {
    initializePlayer();
       }
}
@Override
protected void onStop() {
    super.onStop();
    releasePlayer();
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    // See whether the player view wants to handle media or DPAD keys events.

    return playerView.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);

}


private void initializePlayer() {
    try {
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
        Uri videoURI = Uri.parse(videoURL);
        DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        MediaSource mediaSource = new ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null);
       //playerView.setPlayer(player);

        if (isTimelineStatic) {
            if (playerPosition == C.TIME_UNSET) {
                player.seekToDefaultPosition(playerWindow);
            } else {
                player.seekTo(playerWindow, playerPosition);
            }
        }
        player.prepare(mediaSource);
        player.setPlayWhenReady(shouldAutoPlay);
        medDetails =  setMediaDetails();
        plugin = new CustPlugin(player,tracker,medDetails);
        plugin.startMonitoring();
        playerView.setPlayer(player);
    }catch (Exception e){
        Toast.makeText(this, "Failed to Initialize!", Toast.LENGTH_LONG).show();
        Log.e("Demo Activity"," Exoplayer Error "+ e.toString());
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.exo_play:
            Toast.makeText(this,"Video Resumed",Toast.LENGTH_LONG).show();
            break;
        case R.id.exo_ffwd:
            Toast.makeText(this,"FFWD clicked",Toast.LENGTH_LONG).show();
            break;
        case R.id.exo_prev:
            Toast.makeText(this,"REW clicked",Toast.LENGTH_LONG).show();
            break;
        default:
            break;
    }
}

private void releasePlayer(){
    if (player != null) {
        playbackPosition = player.getCurrentPosition();
        currentWindow = player.getCurrentWindowIndex();
        shouldAutoPlay = player.getPlayWhenReady();
        player.setVideoDebugListener(null);
        player.setAudioDebugListener(null);
        playerPosition = C.TIME_UNSET;
        Timeline timeline = player.getCurrentTimeline();
        if (!timeline.isEmpty() && timeline.getWindow(playerWindow, window).isSeekable) {
            playerPosition = player.getCurrentPosition();
        }
        player.release();
        player = null;
        plugin.stopMonitoring();
    }
}

@SuppressLint("InlinedApi")
private void hideSystemUi() {
    playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    );
}

@Override
public void onVisibilityChange(int visibility) {

}

}// End Class
Ayush Badraj
  • 111
  • 1
  • 1
  • 4
  • please provide layout file which has `<SimpleExoPlayerView>` at line number 32. Also please tell your android version and support version. – Rahul Mar 28 '18 at 11:18
  • Note: I have tried using FrameLayout and Fragment in XML, but still same problem is occurring. – Ayush Badraj Mar 28 '18 at 11:18
  • My android Studio version is 3.0.1 running app on android API 25 in emulator, and my mobile is redmi4 API 25, Also there is only one xml file. – Ayush Badraj Mar 28 '18 at 11:21
  • These are dependencies: compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.jakewharton.timber:timber:4.5.1' compile 'com.android.support:support-v4:25.3.1' compile 'com.android.support:design:25.3.1' compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' compile 'com.google.android.exoplayer:exoplayer:r2.5.2' compile 'com.google.android.gms:play-services-location:9.8.0' – Ayush Badraj Mar 28 '18 at 11:24
  • try declaring `SimpleExoPlayerView_shutter_background_color` in attrsx.xml. This is the property which your android (phone) is unable to find. – Rahul Mar 28 '18 at 11:25
  • I tried using SimpleExoPlayerView_shutter_background_color, but still it is having same issue, i.e., running on emulator but not on phone. – Ayush Badraj Mar 28 '18 at 11:57
  • When I switched the version of exoplayer to 2.1.0 from 2.5.2, i.e., compile 'com.google.android.exoplayer:exoplayer:r2.1.0' from, it is working fine on both phone and emulator. It is working fine for lower versions but not for higher versions. – Ayush Badraj Apr 05 '18 at 06:02

4 Answers4

19

Seems it's a known issue, which the docs say you should use this (found solution here) :

compileOptions {
    targetCompatibility JavaVersion.VERSION_1_8
}

I've tested it now, and it seems to work fine with newest version of ExoPlayer.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
9

Try changing your XML file from:

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/exo_player_view"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/resolution_textView"
android:layout_marginTop="2dp" />

to:

<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/exo_player_view"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/resolution_textView"
android:layout_marginTop="2dp" />

Then in your DemoActivity change:

private SimpleExoPlayerView playerView;

to:

private PlayerView playerView;

Also, make sure that your casting is correct:

playerView = (PlayerView) findViewById(R.id.exo_player_view);

Note that I'm using version 2.7.3:

compile 'com.google.android.exoplayer:exoplayer:2.7.3'
Vid Bregar
  • 148
  • 6
  • after changing to playerView that crash got fixed but other crash occurred :
    E AndroidRuntime: Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatSeekBar cannot be cast to com.google.android.exoplayer2.ui.TimeBar 07-11 02:39:42.313 11914 11914 E AndroidRuntime: at com.google.android.exoplayer2.ui.PlayerControlView.(PlayerControlView.java:311)
    – parul Jul 10 '18 at 21:35
  • Then try replacing AppCompatSeekBar with TimeBar – Vid Bregar Jul 11 '18 at 03:27
  • 1
    I got the same issue and I use `com.google.android.exoplayer2.ui.PlayerView` and latest version of exoplayer. When I changed the version to 2.8.4, it worked fine. How come? See here: https://github.com/google/ExoPlayer/issues/5138 . – android developer Dec 09 '18 at 12:44
0

Because I had several modules, I was earlier adding the dependency to the .app module build.gradle. I finally resolved this by adding the dependency in the appropriate module's build.gradle.

implementation 'com.google.android.exoplayer:exoplayer-core:2.12.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.12.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.12.0'
Sterlingking
  • 190
  • 1
  • 6
-2

Paste compileOptions in android scope as the following:

   android {
        compileOptions{
              sourceCompatibility JavaVersion.VERSION_1_8
              targetCompatibility JavaVersion.VERSION_1_8
       }
    }
Efe Budak
  • 659
  • 5
  • 27
Dev_Anay
  • 1
  • 2