goxr3plus / java-stream-player

🌌Java Advanced Audio Controller Library (WAV, AU, AIFF, MP3, OGG VORBIS, FLAC, MONKEY's AUDIO and SPEEX audio formats )
GNU General Public License v3.0
147 stars 33 forks source link

Can't get addStreamPlayerListener to work #53

Closed Georgieeeee closed 4 years ago

Georgieeeee commented 4 years ago

So I tried creating an instance of the class, and it works fine, except if I try to add a listener, it doesn't listen for any events.

Demo.addStreamPlayerListener(new StreamPlayerListener() {

                        @Override
                        public void statusUpdated(StreamPlayerEvent event) {
                            // TODO Auto-generated method stub
                            System.out.println("output");
                        }

                        @Override
                        public void progress(int nEncodedBytes, long microsecondPosition, byte[] pcmData, Map<String, Object> properties) {
                            // TODO Auto-generated method stub
                            System.out.println("output");

                        }

                        @Override
                        public void opened(Object dataSource, Map<String, Object> properties) {
                            // TODO Auto-generated method stub
                            System.out.println("output");

                        }
                    });

I would greatly appreciate any help on the matter, and thank you for this amazing library.

goxr3plus commented 4 years ago

You used version 9.0.4 right?

And you followed the example :) ?

Probably you didn't attached the Listener to the StreamPlayer like :

streamPlayerInstrance.addStreamPlayerListener(streamPlayerListenerIntance)

/**
 *
 */

import java.io.File;
import java.util.Map;

import com.goxr3plus.streamplayer.enums.Status;
import com.goxr3plus.streamplayer.stream.StreamPlayer;
import com.goxr3plus.streamplayer.stream.StreamPlayerListener;
import com.goxr3plus.streamplayer.stream.StreamPlayerEvent;
import com.goxr3plus.streamplayer.stream.StreamPlayerException;

/**
 * @author GOXR3PLUS
 *
 */
public class Main extends StreamPlayer implements StreamPlayerListener {

    private final String audioAbsolutePath = "Logic - Ballin [Bass Boosted].mp3";

    /**
     * Constructor
     */
    public Main() {

        try {

            // Register to the Listeners
            addStreamPlayerListener(this);

            // Open a File
            // open(new File("...")) //..Here must be the file absolute path
            // open(INPUTSTREAM)
            // open(AUDIOURL)

            // Example
            open(new File(audioAbsolutePath));

            //Seek by bytes
            //seekBytes(500000L);

            //Seek +x seconds starting from the current position
            seekSeconds(15);
            seekSeconds(15);

            /* Seek starting from the begginning of the audio */
            //seekTo(200);

            // Play it
            play();
            //pause();

        } catch (final Exception ex) {
            ex.printStackTrace();
        }

    }

    @Override
    public void opened(final Object dataSource, final Map<String, Object> properties) {

    }

    @Override
    public void progress(final int nEncodedBytes, final long microsecondPosition, final byte[] pcmData,final Map<String, Object> properties) {

//      System.out.println("Encoded Bytes : " + nEncodedBytes);

        // Current time position in seconds:) by GOXR3PLUS STUDIO
        // This is not the more precise way ...
        // in XR3Player i am using different techniques .
        //https://github.com/goxr3plus/XR3Player
        // Just for demostration purposes :)
        // I will add more advanced techniques with milliseconds , microseconds , hours
        // and minutes soon

        // .MP3 OR .WAV
        final String extension = "mp3"; //THE SAMPLE Audio i am using is .MP3 SO ... :)

        long totalBytes = getTotalBytes();
        if ("mp3".equals(extension) || "wav".equals(extension)) {

            // Calculate the progress until now
            double progress = (nEncodedBytes > 0 && totalBytes > 0)
                ? (nEncodedBytes * 1.0f / totalBytes * 1.0f)
                : -1.0f;
//          System.out.println(progress*100+"%");

            System.out.println("Seconds  : " + (int) (microsecondPosition / 1000000) + " s " + "Progress: [ " + progress * 100 + " ] %");

            // .WHATEVER MUSIC FILE*
        } else {
            //System.out.println("Current time is : " + (int) (microsecondPosition / 1000000) + " seconds");
        }

    }

    @Override
    public void statusUpdated(final StreamPlayerEvent streamPlayerEvent) {

        // Player status
        final Status status = streamPlayerEvent.getPlayerStatus();
        //System.out.println(streamPlayerEvent.getPlayerStatus());

        //Examples

        if (status == Status.OPENED) {

        } else if (status == Status.OPENING) {

        } else if (status == Status.RESUMED) {

        } else if (status == Status.PLAYING) {

        } else if (status == Status.STOPPED) {

        } else if (status == Status.SEEKING) {

        } else if (status == Status.SEEKED) {

        }

        //etc... SEE XR3PLAYER https://github.com/goxr3plus/XR3Player for advanced examples
    }

    public static void main(final String[] args) {
        new Main();
    }

}
Georgieeeee commented 4 years ago

It is attached, I basically made an instance of the same class you have above, and added a listener to it in my class using the same way I pasted above your code.

So basically using your code I did the following in my class: Main music = Main();

music.addStreamPlayerListener(new StreamPlayerListener() {

                    @Override
                    public void statusUpdated(StreamPlayerEvent event) {
                        // TODO Auto-generated method stub
                        System.out.println("output");
                    }

                    @Override
                    public void progress(int nEncodedBytes, long microsecondPosition, byte[] pcmData, Map<String, Object> properties) {
                        // TODO Auto-generated method stub
                        System.out.println("output");

                    }

                    @Override
                    public void opened(Object dataSource, Map<String, Object> properties) {
                        // TODO Auto-generated method stub
                        System.out.println("output");

                    }
                });
goxr3plus commented 4 years ago

And it plays music and the statusUpdated method is really never called? I have tested it locally and it works . Do you have other error ?

Georgieeeee commented 4 years ago

No errors, and the statusupdate in the Main class is working fine and outputing to console, but the listener I added in my class using the music.addStreamPlayerListener(new StreamPlayerListener() {

is not listening for any events

Georgieeeee commented 4 years ago

okay it's working now, I don't know why but I just tried running the code again and it's working fine now. Sorry for the trouble and Thank you for making this amazing Library, you're a very good programmer. Thank you again

goxr3plus commented 4 years ago

:) Happy happy happy

Georgieeeee commented 4 years ago

When I try to open and play a new file in the statusUpdated when the first audio file has stopped, it doesn't play the new file, it just says stream has ended

goxr3plus commented 4 years ago

Firstly you have to .close() the first one and then .open() and .play() the next one :)

Georgieeeee commented 4 years ago

oh, okay I will try it

Georgieeeee commented 4 years ago

There is no close function

goxr3plus commented 4 years ago

There is .stop() function.

Georgieeeee commented 4 years ago

I tried .stop(), then .open() in the if (status == Status.STOPPED) {, but it didn't work

Gives me this in the console :

Dec 12, 2019 6:03:40 PM com.goxr3plus.streamplayer.stream.StreamPlayer closeStream INFO: Stream closed

But doesn't play the new audio

goxr3plus commented 4 years ago

No no no ,don't call .stop() inside the if (status == Status.STOPPED) , you have to call it outside of the listener methods .

Do you want to know when a song ends so you start another one right :) ?

Georgieeeee commented 4 years ago

yes That's why I want to do it inside the listener so it knows when the song has ended you are correct, outside it works