How to I achieve ViewPager (next/previous) in Music player App

时光总嘲笑我的痴心妄想 提交于 2020-02-20 04:53:43

问题


I want to know how I can achieve next and previous using swipe via a view pager. The final result I want is the one that Google Play Music has. I have uploaded a picture of what my app is behaving like and also what I expect. I basically cannot get my ViewPager to show me previews of the next and previous tracks. Can anyone please help me with how I can populate the view pager with pages of the tracks I want to play hence achieving that Google play music next/previous swipe in their music playback screen.

 public class PlayPagerAdapter extends FragmentStatePagerAdapter {
    final String POSITION = "position";
    Context mContext;

    public PlayPagerAdapter(FragmentManager fm,Context context) {
        super(fm);
        mContext = context;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = new FragmentNowPlaying();
        Log.e("CHASE MUSIC","The get Item position is " + position);
        Bundle args = new Bundle();
        args.putInt(POSITION, position);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public int getCount() {


        mApp = (Common) mContext.getApplicationContext();


        try {
            if (mApp.getService().getQueue().length !=0 ) {
                return mApp.getService().getQueue().length;
            } else {
                mApp.getService().stopSelf();
                return 0;
            }
        }catch(Exception e){
            e.printStackTrace();
            return 0;
        }


    }

}

Okay now below I have added the code from the fragment particularly code that has the position value from the Fragment Adapter. Please note that the service does most of the heavy lifting i.e Songs from the queue,next,previous,pause,play are all handled by the service.

 public void updateTrackInfo() {

    songPosition = getArguments().getInt(POSITION);
    Log.e("Chase Music SongPos","Song Position is: "+ songPosition);

    if (mService == null) {
        return;
    }
    try {
        long songid = songPosition;
            ((View) mArtistName.getParent()).setVisibility(View.VISIBLE);
            ((View) mAlbumName.getParent()).setVisibility(View.VISIBLE);
            String artistName = mService.getArtistName();
            if (MediaStore.UNKNOWN_STRING.equals(artistName)) {
                artistName = getString(R.string.unknown_artist_name);
            }
            mArtistName.setText(artistName);

            String albumName = mService.getAlbumName();
            long albumid = mService.getAlbumId();
            if (MediaStore.UNKNOWN_STRING.equals(albumName)) {
                albumName = getString(R.string.unknown_album_name);
                albumid = -1;
            }
            mAlbumName.setText(albumName);
            mSongName.setText(mService.getTrackName());
            mAlbumArtHandler.removeMessages(GET_ALBUM_ART);
            mAlbumArtHandler.obtainMessage(GET_ALBUM_ART, new AlbumSongIdWrapper(albumid, songid)).sendToTarget();
            mCoverArt.setVisibility(View.VISIBLE);

        mDuration = mService.duration();
    } catch (RemoteException ex) {
        getActivity().finish();
    }
}

I call the above method in onActivityCreated().

来源:https://stackoverflow.com/questions/32145170/how-to-i-achieve-viewpager-next-previous-in-music-player-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!