我是Android知识的新手,希望您能为我提供帮助。 我尝试制作一些音乐应用程序。
应用程序必须执行以下操作:
因此,我使用了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;
}
}
答案 0 :(得分:0)
尝试通过在私有LoopMediaPlayer()构造函数中删除此createNextMediaPlayer(mResId);