wanted to know how do I keep the stream running

Asked

Viewed 21 times

0

Hi, I was wondering how do I keep the stream running, even if I go to another screen, or exit the application. More do not know how to do. Someone give me a hint and example. Thank you

I’m using Fragmen class When it came out to another Activity I saw in the log this: W/Mediaplayer-JNI: Mediaplayer finalized without being Released

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab1aovivo, container, false);

    imgStream = (ImageView) view.findViewById(R.id.imgStreamId);
    btnTocar = (ImageView)  view.findViewById(R.id.btnExecutarId);
    btnTocar.setImageResource(R.drawable.play);
    imgStream.setVisibility(imgStream.INVISIBLE);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    progressDialog = new ProgressDialog(getActivity());

    btnTocar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(estadoInicial) {
                if(mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                }
                estadoInicial = false;
            } else {
                if(playPause) {
                    new Player().execute(stream);
                } else {
                    if(!mediaPlayer.isPlaying()) {
                        mediaPlayer.start();
                    }
                }
                estadoInicial = true;
                playPause = false;
            }
            checkIsPlaying();
        }
    });
    checkIsPlaying();
    return view;
}

private void checkIsPlaying(){
    if(!estadoInicial) {
        btnTocar.setImageResource(R.drawable.play);
        imgStream.setImageResource(R.drawable.streaming_parado);
    } else {
        btnTocar.setImageResource(R.drawable.stop);
        Glide.with(getActivity()).load(R.drawable.streaming).asGif().into(imgStream);
    }
}

class Player extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... strings) {
        try {
            mediaPlayer.setDataSource(strings[0]);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    estadoInicial = false;
                    playPause = true;
                    btnTocar.setImageResource(R.drawable.play);
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                }
            });
            mediaPlayer.prepare();
            prepared = true;

        } catch (IOException e) {
            e.printStackTrace();
            prepared = false;
        }
        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        if(progressDialog.isShowing()) {
            progressDialog.cancel();
        }
        btnTocar.setImageResource(R.drawable.stop);
        mediaPlayer.start();
        imgStream.setVisibility(imgStream.VISIBLE);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Glide.with(getActivity()).load(R.drawable.carregando).asGif().into(btnTocar);
        progressDialog.setMessage("Carregando Streaming...");
        progressDialog.show();
    }
}

@Override
public void onPause() {
    super.onPause();
    Toast.makeText(getActivity(), "atc pause", Toast.LENGTH_SHORT).show();
    if(estadoInicial) {
        mediaPlayer.start();
        Toast.makeText(getActivity(), "play-pause", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onResume() {
    super.onResume();
    if(estadoInicial) {
        mediaPlayer.start();
    }
}

@Override
public void onDestroy() {
    if(prepared) {
        mediaPlayer.release();
        mediaPlayer = null;
    }
    super.onDestroy();
}
  • I recommend you read on services on android

  • Thanks man, I read a read, but I was a little lost in how to apply inside my code. I’ll do another search and see what I find. more question one more thing, in this application I added a notification, for when it is execuntado shows in the status bar, well, what I can not, fooi by clicking on this notification open the application, Voce can tell me how or have some example.

  • the notification method: private void notification() { Notificationcompat.Builder Builder = new Notificationcompat.Builder(getActivity(); Builder.setSmallIcon( R.drawable.icon_notification ) . setContentTitle( "Rpfm 105.3" ) . setContentText( "Live" ) . setAutoCancel( false ); int id = 1; Notificationmanager notifyManager = (Notificationmanager) getActivity(). getSystemService(Context.NOTIFICATION_SERVICE); notifyManager.notify( id, Builder.build() ); }

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.