Jump to content

this is my last post about any kind of music player ever but I really need help. Im working on basic android music player application and Im having trouble to create NEXt and PREVIOUS buttons. So i pretty pretty please if anyone can get me out of that.I have tried something more about that downhere THANK YOU. I have created this yet:

Main Application Java file

    
    

    
    public class MainActivity extends ListActivity {
    
        private static final int UPDATE_FREQUENCY = 500;
        private static final int STEP_VALUE = 4000;
        private MediaCursorAdapter mediaAdapter = null;
        private TextView selectedFile= null;
        private SeekBar seekbar = null;
        private MediaPlayer player = null;
        private ImageButton playButton = null;
        private ImageButton previousButton = null;
        private  ImageButton nextButton = null;
        private boolean isStarted = true;
        private  String currentFile = "";
        private boolean isMovingseekBar = false;
        private final Handler handler = new Handler();
        private final Runnable updatePositionRunnable = new Runnable() {
            public  void run() {
                updatePosition();
    
            }
        };
    
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
                        1);
    
    
    
    
         selectedFile = (TextView) findViewById(R.id.selectedfile);
            seekbar = (SeekBar) findViewById(R.id.seekbar);
            playButton = (ImageButton) findViewById(R.id.play);
            previousButton = (ImageButton) findViewById(R.id.previous);
            nextButton = (ImageButton) findViewById(R.id.next);
    
            player = new MediaPlayer();
    
            player.setOnCompletionListener(onCompletion);
            player.setOnErrorListener(onError);
            seekbar.setOnSeekBarChangeListener(seekBarChanged);
            Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
    
            if (null != cursor) {
                cursor.moveToFirst();
    
                mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
                setListAdapter(mediaAdapter);
                playButton.setOnClickListener(onButtonClick);
                nextButton.setOnClickListener(onButtonClick);
                previousButton.setOnClickListener(onButtonClick);
            }
        }
        @Override
        protected  void onListItemClick(ListView list, View view, int position,long id) {
            super.onListItemClick(list, view, position, id);
            currentFile = (String) view.getTag();
            startPlay(currentFile);
        }
        @Override
        protected void onDestroy(){
            super.onDestroy();
    
            handler.removeCallbacks(updatePositionRunnable);
            player.stop();
            player.reset();
            player.release();
    
            player = null;
        }
        private  void startPlay(String file) {
            selectedFile.setText(file);
            seekbar.setProgress(0);
    
            player.stop();
            player.reset();
    
            try {
                player.setDataSource(file);
                player.prepare();
                player.start();
            } catch (IllegalArgumentException e){
                e.printStackTrace();
            } catch (IllegalStateException e){
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }
    
            seekbar.setMax(player.getDuration());
            playButton.setImageResource(android.R.drawable.ic_media_pause);
    
            updatePosition();
    
            isStarted = true;
    
        }
    
        private void stopPlay() {
            player.stop();
            player.reset();
            playButton.setImageResource(android.R.drawable.ic_media_play);
            handler.removeCallbacks(updatePositionRunnable);
            seekbar.setProgress(0);
    
            isStarted = false;
        }
        private void updatePosition() {
            handler.removeCallbacks(updatePositionRunnable);
    
    //        seekbar.setSecondaryProgressTintMode(player.getCurrentPosition());
            handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY);
        }
    
    
        private class MediaCursorAdapter extends SimpleCursorAdapter{
    
            MediaCursorAdapter(Context context, int layout, Cursor c){
                super(context, layout,c,
                        new String[] {MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
                        new int[] {R.id.displayname, R.id.title,R.id.duration});
            }
    
            @Override
            public void bindView(View view,Context context, Cursor cursor) {
                TextView title = (TextView) view.findViewById(R.id.title);
                TextView name = (TextView) view.findViewById(R.id.displayname);
                TextView duration = (TextView) view.findViewById(R.id.duration);
    
                name.setText(cursor.getString(
                        cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));
    
                title.setText(cursor.getString(
                        cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
    
                view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
            }
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                LayoutInflater inflater = LayoutInflater.from(context);
                View v = inflater.inflate(R.layout.listitem, parent, false);
    
                bindView(v, context, cursor);
    
                return v;
            }
        }
    
    
    
        private View.OnClickListener onButtonClick  = new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.play: {
                        if (player.isPlaying()) {
                            handler.removeCallbacks(updatePositionRunnable);
                            player.pause();
                            playButton.setImageResource(android.R.drawable.ic_media_play);
                        } else {
                            if (isStarted) {
                                player.start();
                                playButton.setImageResource(android.R.drawable.ic_media_pause);
                                updatePosition();
                            } else {
                                startPlay(currentFile);
                            }
                        }
                        break;
                    }
                    case R.id.next: {
                        int seekto = player.getCurrentPosition() + STEP_VALUE;
    
                        if (seekto < player.getDuration())
                            seekto = player.getDuration();
                        player.pause();
                        player.seekTo(seekto);
                        player.start();
    
                        break;
                    }
                    case R.id.previous: {
                        int seekto = player.getCurrentPosition() - STEP_VALUE;
    
                        if (seekto < player.getDuration())
                            seekto = player.getDuration();
    
                        player.pause();
                        player.seekTo(seekto);
                        player.start();
    
                        break;
    
                    }
                }
            }
        };
        private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
    
            @Override
            public void onCompletion(MediaPlayer mp) {
                stopPlay();
            }
        };
    
        private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {
    
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
    
                return false;
            }
        };
    
    
        private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isMovingseekBar = false;
            }
    
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fomUser) {
                if (isMovingseekBar) {
                    player.seekTo(progress);
                    Log.i("OnSeekBarChangeListener", "onProgressChange");
                }
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isMovingseekBar = true;
            }
        };
    
    }

I have already did switch with NEXT and PREVIOUS buttons and I have tried to make them work BUT I ended up with getting at the and or beggining of the song not the NEXT or PREVIOUS. 

Link to post
Share on other sites

29 minutes ago, James Evens said:

Thx for the edit. Way better to read now.

You basicly just jumping to the end of the track. As  the current position - stepalue is always smaller then getduration. For next your seekto can be outsite of the track duration. If the button is fired in the last STEP_value (4 sec.) of a track (change < to >).

 

If you have a tracklist just set the filename to the previos or next item on the list and send the play command.

Thank you! But I still cant get into it. I have my list of song created by "external_content_URi" with cursor so I dont know that i actually can place int STEP_VALUE to jump into next song :/ 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×