Moving to the next track in mediplayer



  • Переход к след. треку по завершению текущего осуществляю так:
    public class PageFragment extends Fragment implements MediaPlayer.OnCompletionListener {
    
        //массивы мелодий
        private int[] soundsRawResIds = new int[]{R.raw.belarus, R.raw.russian, R.raw.japan,
                R.raw.litva, R.raw.england, R.raw.finlandia, R.raw.france};
        //каждой кнопке присвоена своя мелодия
        private int[] buttonIds = new int[]{R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7};
        private int[] fragments = new int[]{R.id.imageView1, R.id.imageView2, R.id.imageView3};
    
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    
            fab();
            pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);
    
    
            playerText = (TextView) getActivity().findViewById(R.id.textPlayer);
    
            anim = AnimationUtils.loadAnimation(getContext(), R.anim.clickbutton);
            potehanim = AnimationUtils.loadAnimation(getContext(), R.anim.potehkianim);
    
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            rootViewB = inflater.inflate(R.layout.fragment2, container, false);
            seekBar = (SeekBar) rootViewB.findViewById(R.id.sbar);
            imageButton = (ImageButton) rootViewB.findViewById(R.id.button_play);
            View rootView = inflater.inflate(R.layout.fragment, container, false);
            startMediaPlayer();
            setSeekBar();
            playAllSongs();
            mediaPlayer.setOnCompletionListener(this);
    
    
            for (int i = 0; i < fragments.length; i++) {
                arr_imageA[i] = (ImageView) rootView.findViewById(fragments[i]);
    
                arr_imageA[i].setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
    
                        v.startAnimation(potehanim);
    
                        switch (v.getId()) {
                            case R.id.imageView1:
                                startActivity(getActivity(), a1.class);
                                break;
                            case R.id.imageView2:
                                startActivity(getActivity(), a2.class);
                                break;
                            case R.id.imageView3:
                                startActivity(getActivity(), a3.class);
                                break;
    
                        }
                    }
                });
    
                if (pageNumber == 1) {
                    for (int index = 0; index < buttonIds.length; index++) {
                        arr_imageB[index] = (ImageView) rootViewB.findViewById(buttonIds[index]);
                    }
    
                    for (int j = 0; j < arr_imageB.length; j++) {
    
                        arr_imageB[j].setOnClickListener(new View.OnClickListener() {
    
    
                            @Override
                            public void onClick(View v) {
                                stopAnimation();
                                switch (v.getId()) {
                                    case R.id.b1: {
                                        v.startAnimation(anim);
    
                                        playSample(soundsRawResIds[0]);
                                        break;
                                    }
                                    case R.id.b2: {
                                        v.startAnimation(anim);
                                        playSample(soundsRawResIds[1]);
                                        break;
                                    }
                                    case R.id.b3: {
                                        v.startAnimation(anim);
                                        playSample(soundsRawResIds[2]);
                                        break;
                                    }
                                    case R.id.b4: {
                                        v.startAnimation(anim);
                                        playSample(soundsRawResIds[3]);
                                        break;
                                    }
                                    case R.id.b5: {
                                        v.startAnimation(anim);
                                        playSample(soundsRawResIds[4]);
                                        break;
                                    }
                                    case R.id.b6: {
                                        v.startAnimation(anim);
                                        playSample(soundsRawResIds[5]);
                                        break;
                                    }
                                    case R.id.b7: {
                                        v.startAnimation(anim);
                                        playSample(soundsRawResIds[6]);
                                        break;
                                    }
                                }
                            }
    
                        });
                    }
                    return rootViewB;
                }
            }
            return rootView;
        }
    //обновляю сеекбар
        private Runnable onEverySecond = new Runnable() {
    
            @Override
            public void run() {
    
                if (seekBar != null) {
                    seekBar.setProgress(mediaPlayer.getCurrentPosition());
                }
    
                if (mediaPlayer.isPlaying()) {
                    seekBar.postDelayed(onEverySecond, 1000);
                }
    
            }
        };
    
    //воспроизвожу мелодии из case
        public void startMediaPlayer() {
    
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    seekBar.setMax(mp.getDuration());
                    seekBar.postDelayed(onEverySecond, 1000);
                    mediaPlayer.start();
    
                }
            });
    
        }
    
        public void setSeekBar() {
            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onProgressChanged(SeekBar seekBars, int progress, boolean fromUser) {
    
                    if (mediaPlayer != null && fromUser) {
                        mediaPlayer.seekTo(progress);
                    }
    
    
                }
            });
        }
    //метод в котором по нажатию на кнопку играют все мелодии из массива(подряд)
        public void playAllSongs() {
    
            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.startAnimation(anim);
                   idx = (idx + 1) ;
                    AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(soundsRawResIds[idx]);
    
                    try {
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        afd.close();
                    } catch (IllegalArgumentException e) {
                        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
                    } catch (IllegalStateException e) {
                        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
                    } catch (IOException e) {
                        Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
                    }
                }
            });
        }
    //запуск активти с задержкой
        public void startActivity(final Context activityContext, final Class<? extends Activity> activityClass) {
            new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    startActivity(new Intent(activityContext, activityClass));
                }
            }, 200);
        }
    
    
    
        public void stopAnimation() {
            for (int index = 0; index < buttonIds.length; index++) {
    
                arr_imageB[index].clearAnimation();
    
            }
        }
    
        private void playSample(int resid) {
            AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(resid);
    
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                stopAnimation();
    
            } else {
    
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                    mediaPlayer.prepareAsync();
    
                    afd.close();
    
    
                } catch (IllegalArgumentException e) {
                    Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
                } catch (IllegalStateException e) {
                    Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
                } catch (IOException e) {
                    Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
                }
            }
    
        }
    
    //отключение звука
        public void setVolume(float left, float right) {
            mediaPlayer.setVolume(left, right);
        }
    
        @Override
        public void onPause() {
            /*if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                mediaPlayer.seekTo(0);
            }*/
            Notify();
            super.onPause();
        }
    
        @Override
        public void onStop() {
    
            super.onStop();
        }
        @Override
        public void onResume() {
    
            super.onResume();
        }
    
        @Override
        public void onCompletion(MediaPlayer mp) {
            mediaPlayer.reset();
    
        }
    }
    


  • You know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you know, you http://www.codota.com/android/methods/android.media.MediaPlayer/setOnCompletionListener ♪

    And the line from the league is, as I recall, purely good information, nothing related to the life cycle of audio-trafficing.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2