PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 5, 2022

[FIXED] How to connect ExoPlayer inside Service with ExoPlayerView inside activity?

 July 05, 2022     android, exoplayer, pass-by-reference     No comments   

Issue

I want to add "Fullscreen" button to the standard SimpleExoPlayer implementation from Google. I added the ImageButton and everything works well so far.

Then I decided that fullscreen will work in next way: after pressing on button, I open new activity with fullscreen SimpleExoPlayerView, pass there video uri and current position, initialize player and seek for given position. It works, but the player re-initialization takes 1-3 seconds depending on a device what don't want to have.

Now I would like to have an instance of player inside intent service and just reattach existing player to any view that wants to show it (preview or fullscreen view), like this:

mPlayerView.setPlayer(mPlayer);

The problem is that service will have the player and activity will have the view. No one of them will have both to be able to attach player to view. As a workaround, I think about making that the service class may have a static link to the player, so activity will be able to get it via static reference. But this seems like some code smell and I don't know if there won't be problems with communications between threads.

So, how can I pass the player from Service (that is not Serializable or Parcelable) to Activity or how can I pass a player view to the Service?


Solution

exoPlayer handle fullscreen good enough - try to change your layout params of playerView to match the screen on rotation or pressing button.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int width;
    int height;

    View decorView = getWindow().getDecorView();
    int uiOptions;

    if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Resources r = getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, r.getDisplayMetrics());
        width = CoordinatorLayout.LayoutParams.MATCH_PARENT;
        height = (int)px;
        uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
    } else {
        uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    }

    decorView.setSystemUiVisibility(uiOptions);
    final CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(width, height);
    mAppBar.setLayoutParams(lp);
}

Also don't forget to add this to your activity manifest to prevent activity reload:

android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"


Answered By - Sough
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing