Android音乐应用

时间:2018-10-28 10:25:03

标签: android android-mediaplayer media-player

我是Android知识的新手,希望您能为我提供帮助。 我尝试制作一些音乐应用程序。

应用程序必须执行以下操作:

  • 当用户按下播放按钮时,一个音频文件(称为“ mainFile”)必须循环播放。
  • 播放“ mainFile”并且用户按下fillin按钮时,另一个音频文件称为“ fillinFile”, 必须在最后一个循环结束后立即开始。

因此,我使用了Not able to achieve Gapless audio looping so far on Android中的“ LoopMediaPlayer” 其实效果很好。

我的问题: 如果我按下fillin按钮,则该应用会在“ fillinFile”开始播放之前循环两次“ mainFile”。

这是我的代码:

public class SoundsFragment extends Fragment implements View.OnClickListener {

    public static String TAG = "PLAYER";
    private XButton fillIn;
    private XButton stop;
    private XButton play;
    private LoopMediaPlayer mediaPlayerMain;
    private LoopMediaPlayer mediaPlayerFillIn;
    private Taal playingTaal;

    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.sounds, container, false);

        bindButtons();
        return view;
    }


    @Override
    public void onClick(View v) {
        //stop();
        XButton button = (XButton) v;
        switch (button.getId()) {
            case R.id.fillin:
                playFillIn();
                break;
            case R.id.play:
                playMain();
                break;
            default:
        }
    }

    private void playMain() {
        mediaPlayerMain = LoopMediaPlayer.create(getContext(), R.raw.d_vb_01, R.raw.d_vb_01_var);

    }

    private void playFillIn() {
        mediaPlayerMain.setStartedFollowers(true);
    }


    private void bindButtons() {
        fillIn = (XButton) view.findViewById(R.id.fillin);
        stop = (XButton) view.findViewById(R.id.stop);
        play = (XButton) view.findViewById(R.id.play);
        fillIn.setOnClickListener(this);
        stop.setOnClickListener(this);
        play.setOnClickListener(this);
    }

}

public class LoopMediaPlayer {

    public static final String TAG = LoopMediaPlayer.class.getSimpleName();

    private Context mContext = null;

    private int mFollowersResId= 0;

    private int mCounter = 1;

    private MediaPlayer mCurrentPlayer = null;
    private MediaPlayer mNextPlayer = null;

    private boolean mStartedFollowers = false;
    private boolean mStartedMain = false;
    private int mResId = 0;


    public static LoopMediaPlayer create(Context context, int mainResId, int followersResId) {
        return new LoopMediaPlayer(context, mainResId, followersResId);
    }

    private LoopMediaPlayer(Context context, int mainResId, int followersResId) {

        mContext = context;
        mResId = mainResId;

        mFollowersResId = followersResId;

        mCurrentPlayer = MediaPlayer.create(mContext, mResId);

        mCurrentPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mCurrentPlayer.start();
            }
        });

        createNextMediaPlayer(mResId);
    }

    private void createNextMediaPlayer(int id) {
        mNextPlayer = MediaPlayer.create(mContext, id);
        mCurrentPlayer.setNextMediaPlayer(mNextPlayer);
        mCurrentPlayer.setOnCompletionListener(onCompletionListener);
    }

    private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {

            if(mStartedFollowers){
                mResId = mFollowersResId;
            }

            mCurrentPlayer = mNextPlayer;
            createNextMediaPlayer(mResId);
            mediaPlayer.release();

        }
    };

    public void setStartedFollowers(boolean started){

        mStartedFollowers = started;

    }

}

1 个答案:

答案 0 :(得分:0)

尝试通过在私有LoopMediaPlayer()构造函数中删除此createNextMediaPlayer(mResId);