Search code examples
javaandroidmedia-playerrunnablerepeat

Programming Music Player A-B repeat


I am trying to write an A-B repeat function for my Media Player. I have already tried a Runnable function with a conditional if statement when it gets to the B marker it will go back to the start marker by saving the position in the audio stream with mediaplayer.getCurrentPosition() function. I also tried a while loop which also yielded unsatisfactory results with the application freezing. Here is the current code I have for a method such as this.

public void aButton() {
        start = mediaPlayer.getCurrentPosition();
    }

    public void bButton() {
        pressedCount += 1;
        System.out.println("AB Button Count: " + pressedCount);
        stop = mediaPlayer.getCurrentPosition();
        mediaPlayer.seekTo(start);
        if(mediaPlayer.getCurrentPosition() == stop){
            mediaPlayer.seekTo(start);
        }
    }

Solution

  • I ended up solving my own problem I decided to write a runnable function with a handler in my program to accommodate a while loop without having to go through exception handling. I set counters for both a and b splice positions in the audio stream. I made sure both buttons had been pressed only once using a counter aCount += 1; and bCount += 1. If they had both been pressed the program would pass control to the Runnable function that checks the getCurrentPosition() every 100 milliseconds, and if the stream is at or past the stop position(defined at the beginning of B button method) it will seekto() the start position defined at the beginning of A button method. Then I set methods for resuming normal playback if either button had been pressed more than once.

    Here is the code showing the changes I made to the code:

    public void aButton() {
            start = mediaPlayer.getCurrentPosition();
    
            if (aCount > 1 && bCount == 1) {
                aCount = 0;
                bCount = 0;
            }
            aCount += 1;
    
        }
    
        public void bButton() {
            stop = mediaPlayer.getCurrentPosition();
    
            if (bCount > 1 && aCount >= 1) {
                bCount = 0;
                aCount = 0;
            }
    
            if (aCount != 1) {
                Toast.makeText(this, "Please press A first", Toast.LENGTH_SHORT).show();
                bCount = 0;
            }
            if(bCount == 0 && aCount == 1){
                bCount += 1;
            }
    
            System.out.println("AB Button Count: " + bCount);
            /*mediaPlayer.seekTo(start);*/
            abLoopHandler.removeCallbacks(abLoop);
            abLoopHandler.postDelayed(abLoop, 100);
        }
    
        private Runnable abLoop = new Runnable() {
            @Override
            public void run() {
                if (aCount == 1 && bCount == 1) {
                    int currPos = mediaPlayer.getCurrentPosition();
                    if (currPos >= stop) {
                        mediaPlayer.seekTo(start);
                    }
                    abLoopHandler.postDelayed(this, 100);
                }
            }
        };
    

    Hope this helps people with this same issue!