Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress Not showing when i declare progress layout as static #12

Open
kp5293 opened this issue Apr 11, 2018 · 0 comments
Open

Progress Not showing when i declare progress layout as static #12

kp5293 opened this issue Apr 11, 2018 · 0 comments

Comments

@kp5293
Copy link

kp5293 commented Apr 11, 2018

Hello
I used your library with with view pager. when audio is playing and swipe then its working perfect but when u again swipe on previous progress will be continue. please help me.
My Code Is As below

DisplayPictureDetail.DpFragmentPagerAdapter dpFragmentPagerAdapter;
ViewPager viewPager;
@SuppressLint("StaticFieldLeak")
Toolbar mToolbar;
private static ArrayList DpDetailArl;
int selectedPosition = 0, pos;
static String Title, CatType, downloadUrl;
@SuppressLint("StaticFieldLeak")
static RelativeLayout mRowDetailLayout;

static int currentDuration = 0, mIntFavCounter;
static Activity activity;
DatabaseHandler db;
static MediaPlayer mediaPlayer;
FloatingActionButton mFabRingtone, mFabNotification, mFabAlarm;
SparkButton mImgFavCounter;
TextView mRowtxtFav;
int CurrentId, CurrentDownloadCount;
static CategoryModel currentTrack = null;
static boolean isPlaying = false;
static ProgressLayout progressLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_pager);
    activity = DisplayPictureDetail.this;
    if (Connectivity.getInstance(DisplayPictureDetail.this).isOnline()) {
        Log.d("RESULT", "------------------DP DETAIL Page Start------------------");

        viewPager = (ViewPager) findViewById(R.id.pager);
        mImgFavCounter = (SparkButton) findViewById(R.id.heart_button);
        mRowtxtFav = (TextView) findViewById(R.id.mRowtxtFav);
        mToolbar = (Toolbar) findViewById(R.id.mToolbar);
      
        setSupportActionBar(mToolbar);
        mediaPlayer = new MediaPlayer();
        db = new DatabaseHandler(activity);
        if (mToolbar != null) {
            mToolbar.setTitleTextColor(Color.WHITE);
            mToolbar.setSubtitleTextColor(Color.WHITE);
            mToolbar.setBackgroundColor(Color.TRANSPARENT);
            getSupportActionBar().setTitle("Title");
        }

        Bundle extra = getIntent().getBundleExtra("extra");
        if (extra != null) {
            DpDetailArl = (ArrayList<CategoryModel>) extra.getSerializable("objects");
            pos = getIntent().getIntExtra("position", 0);
        }

        if (DpDetailArl != null) {
            dpFragmentPagerAdapter = new DpFragmentPagerAdapter(getSupportFragmentManager());
            viewPager.setAdapter(dpFragmentPagerAdapter);
            dpFragmentPagerAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(activity, "No Data Found..", Toast.LENGTH_SHORT).show();
        }
        setCurrentItem(pos);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                    isPlaying = false;
                }
            }

            @Override
            public void onPageSelected(int position) {
                displayMetaInfo(position);
                GradientDrawable gd1 = Config.generateGradiantRandom(DisplayPictureDetail.this);
                viewPager.setBackground(gd1);
                mRowDetailLayout.setBackground(gd1);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
                

    } else {

// new AlertDialog(DisplayPictureDetail.this, getString(R.string.alert_title), getString(R.string.inernet_alert_message),
// getString(R.string.alert_pos_btn), new NavigationMainActivity());
}
}

private void setCurrentItem(int position) {
    viewPager.setCurrentItem(position, false);
    displayMetaInfo(position);
    selectedPosition = position;
}

private void displayMetaInfo(int position) {
    CategoryModel image = DpDetailArl.get(position);
    downloadUrl = image.getUrl();
    CurrentId = image.getId();
    CurrentDownloadCount = image.getDownloads();
      
}

public void onBackPressed() {
    super.onBackPressed();
    if (mediaPlayer != null) {
        mediaPlayer.stop();

    }
}

public void onPause() {
    super.onPause();
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
    }
}

public void onStop() {
    super.onStop();
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
        mediaPlayer.reset();
    }
}

public void onDestroy() {
    super.onDestroy();
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
        mediaPlayer.reset();
    }
}

public class DpFragmentPagerAdapter extends FragmentPagerAdapter {

    public DpFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return DpDetailArl.size();
    }

    @Override
    public Fragment getItem(int position) {
        SwipeFragment fragment = new SwipeFragment();
        return SwipeFragment.newInstance(position);
    }
}

public static class SwipeFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View swipeView = inflater.inflate(R.layout.display_picture_detail, container, false);

        Bundle args = getArguments();
        final int position = args.getInt("position");

        final DatabaseHandler db = new DatabaseHandler(getActivity());
        final ImageView ivIcon;
        final int SECOND_MS = 1000;
        final Handler mHandler = new Handler();

        final Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                currentDuration += 1;
                mHandler.postDelayed(this, SECOND_MS);
            }
        };
        final CategoryModel track = DpDetailArl.get(position);
        mRowDetailLayout = (RelativeLayout) swipeView.findViewById(R.id.mRowDetailLayout);
        progressLayout = (ProgressLayout) swipeView.findViewById(R.id.progressLayout);
        ivIcon = (ImageView) swipeView.findViewById(R.id.ivIcon);

        progressLayout.setMaxProgress(DpDetailArl.get(position).getDurationInSec());

        Title = DpDetailArl.get(position).getName();
        CatType = DpDetailArl.get(position).getCat_type();

        ivIcon.setBackgroundResource(R.drawable.ic_play_white);

        if (currentTrack != null && currentTrack == track) {
            ivIcon.setBackgroundResource(isPlaying ? R.drawable.ic_pause_white : R.drawable.ic_play_white);
            progressLayout.setCurrentProgress(currentDuration);
            if (isPlaying) progressLayout.start();
        } else {
            progressLayout.cancel();
        }
        ivIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String AUDIO_PATH = DpDetailArl.get(position).getUrl();

                if (track != currentTrack) {
                    currentTrack = track;
                    mHandler.removeCallbacks(mRunnable);
                    currentDuration = 0;
                }
                if (!progressLayout.isPlaying()) {
                    isPlaying = true;
                    try {
                        mediaPlayer.reset();
                        if (mediaPlayer != null) {
                            mediaPlayer.setDataSource(AUDIO_PATH);
                            mediaPlayer.prepare();
                            mediaPlayer.start();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    progressLayout.start();
                    mHandler.postDelayed(mRunnable, 0);
                    ivIcon.setBackgroundResource(R.drawable.ic_pause_white);
                    int UpdateViewCount = DpDetailArl.get(position).getViews() + 1;
                    Functions.updateDpCounter(activity, Config.KEY_ID, String.valueOf(DpDetailArl.get(position).getId()),
                            Config.KEY_VIEWS, String.valueOf(UpdateViewCount), Config.VIEWS_COUNTER_UPDATE_URL);

                } else {

                    isPlaying = false;
                    mediaPlayer.stop();

                    progressLayout.setCurrentProgress(0);
                    progressLayout.cancel();
                    mHandler.removeCallbacks(mRunnable);
                    ivIcon.setBackgroundResource(R.drawable.ic_play_white);

// ivIcon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

                }
            }
        });
        progressLayout.setProgressLayoutListener(new ProgressLayoutListener() {
            @Override
            public void onProgressCompleted() {
                progressLayout.stop();
               ivIcon.setBackgroundResource(R.drawable.ic_play_white);
            }

            @Override
            public void onProgressChanged(int seconds) {
                Log.d("Result", "Pogress Mili:-" + seconds);
            }
        });


        return swipeView;
    }


    static SwipeFragment newInstance(int position) {
        SwipeFragment swipeFragment = new SwipeFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        swipeFragment.setArguments(args);
        return swipeFragment;
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant