Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next and play twice #8

Closed
hitmacreeed opened this issue Jun 20, 2017 · 3 comments
Closed

Next and play twice #8

hitmacreeed opened this issue Jun 20, 2017 · 3 comments

Comments

@hitmacreeed
Copy link

Hi made a playlist(next button) for this player since it does not have one
and on the button play why i have to click twice?

@hitmacreeed
Copy link
Author

hitmacreeed commented Jun 20, 2017

`

public class Teste_Music extends BaseActivity {
AudioPlayerView audioPlayerView;
Button tocar;
Button next;

ArrayList<Musica> list;
Musica atualMusica;
int indexMusic = 0;

TextView results;
String JsonURL = "https://.../songs/music.json";
String src;
RequestQueue requestQueue;
StatusMusica status;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame);
    getLayoutInflater().inflate(R.layout.teste_music, contentFrameLayout);
    setTitle("Music");

    requestQueue = Volley.newRequestQueue(this);

    results = (TextView) findViewById(R.id.txtDisplay);
    list = new ArrayList<>();

    JsonArrayRequest arrayreq = new JsonArrayRequest(JsonURL,

            new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    try {

                        JSONObject colorObj = response.getJSONObject(0);

                        JSONArray colorArry = colorObj.getJSONArray("musicas");

                        for (int i = 0; i < colorArry.length(); i++) {

                            JSONObject jsonObject = colorArry.getJSONObject(i);

                            Musica music = new Musica();


                             src = jsonObject.getString("src");

                            music.url = src;

                            list.add(music);
                        }

                        atualMusica = list.get(0);

                        String data = "";

                        for(int i = 0; i < list.size(); i++){
                            data += "\n"+list.get(i).url;
                        }

                        results.setText(data);
                    }

                    catch (JSONException e) {

                        e.printStackTrace();
                    }
                }
            },

            new Response.ErrorListener() {
                @Override

                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "Error");

                        }
                            }
                                 );
                requestQueue.add(arrayreq);

    audioPlayerView = (AudioPlayerView) findViewById(R.id.player);

    tocar = (Button) findViewById(R.id.parar);
    next = (Button) findViewById(R.id.proximo);


    tocar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {

                if (status == StatusMusica.STOPPED){

                    status = StatusMusica.PLAYING;
                    audioPlayerView.toggleAudio();
                }else{

                    status = StatusMusica.STOPPED;
                }

                audioPlayerView.withUrl(atualMusica.url);

            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    });


    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            audioPlayerView.destroy();


            if (indexMusic >= list.size()-1){
                return;
            }

            indexMusic++;
            atualMusica = list.get(indexMusic);

            try {

                if (status == StatusMusica.STOPPED){

                    status = StatusMusica.PLAYING;

                }else{

                    status = StatusMusica.PAUSE;
                    audioPlayerView.toggleAudio();

                }


                audioPlayerView.withUrl(atualMusica.url);



            } catch (Exception e) {
                e.printStackTrace();

            }


        }
    });
    audioPlayerView.setOnAudioPlayerViewListener(new AudioPlayerView.OnAudioPlayerViewListener() {
        @Override
        public void onAudioPreparing() {
            //.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAudioReady() {
            //.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAudioFinished() {
       // next.performClick();

        }

        @Override
        public void onError(Exception e) {

        }
    });

}


@Override
protected void onDestroy() {
    audioPlayerView.destroy();
    super.onDestroy();
}

class Musica {
    String url;

}

public enum StatusMusica {
    PLAYING,
    STOPPED,
    PAUSE
}
}

`

@HugoMatilla
Copy link
Owner

There is some problem with the play twice, I didn't have the time to fix it. But you cab check this issue, it might be helpful. #5

@hitmacreeed
Copy link
Author

hitmacreeed commented Jun 21, 2017

thanks, but solved by doing this

`

        tocar.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

        @Override

        public void onClick(View v) {
            try {
                status = playing ? StatusMusica.PLAYING : StatusMusica.PAUSE;
                audioPlayerView.withUrl(atualMusica.url);
                audioPlayerView.toggle(tocar, status);
                playing = !playing;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants