如何在音乐播放器应用程序中实现ViewPager(下一个/上一个)

时间:2015-08-21 16:25:06

标签: android android-viewpager android-music-player

我想知道如何通过视图寻呼机使用滑动来实现下一个和上一个。我想要的最终结果是Google Play音乐的结果。我已经上传了我的应用程序的行为以及我期望的图片。我基本上无法让我的ViewPager向我展示下一首和上一首曲目的预览。任何人都可以帮助我如何使用我想要播放的曲目页面填充视图寻呼机,从而实现Google在其音乐播放屏幕中播放下一首/上一首的音乐。

The image on the Left is google play music and on the right is my music player app.

 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;
        }


    }

}

现在好了,我已经添加了片段中的代码,特别是具有片段适配器位置值的代码。请注意,该服务完成了大部分繁重的工作,即队列中的歌曲,下一个,上一个,暂停,播放都由服务处理。

 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();
    }
}

我在onActivityCreated()中调用上述方法。

0 个答案:

没有答案