Use Service to continue player operation when class changes

Asked

Viewed 66 times

1

I want to use for when to change Fragment continue running the audio and when to class the ! isPlaying keep popping up the requested pause. How to do this with Service?

import android.app.Fragment;
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.IOException;
import java.net.URL;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;

/**
 * Created by Z0NEN on 10/22/2014.
 */
public class menu1_Fragment extends Fragment {
    View rootview;
    protected static final String TAG = null;
    private AudioManager audioManager;
    private StreamingMediaPlayer audioStreamer;
    private boolean isPlaying;
    private ImageButton playButton;
    private String urlStreaming;
    private menu1_Fragment station;
    public menu1_Fragment(){}


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

        //    playButton = ((ImageButton).findViewById(R.id.play_button));
        playButton = (ImageButton) rootview.findViewById(R.id.play_bt);

        this.playButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                if (isPlaying) {
                    audioStreamer.interrupt();
                    playButton.setImageResource(R.drawable.play);
                }
                if (!isPlaying) {
                    startStreamingAudio();
                    playButton.setImageResource(R.drawable.pause);
                }
                isPlaying = !isPlaying;
            }
        });
        String str = "http";
//Corrigindo o bug do ICY (Icecast, tipo de transmissão depois do API 18)
        if (Build.VERSION.SDK_INT >= 19) {
            setupIcyURLStreamHandler();
            str = "icy";
        }
        urlStreaming = (str + "://sh.upx.com.br:10369");
        //   this.audioManager = ((AudioManager)getSystemService(Context.AUDIO_SERVICE));
        this.audioManager = (AudioManager) (getActivity().getSystemService(Context.AUDIO_SERVICE));
        int i = this.audioManager.getStreamMaxVolume(3);
        int j = this.audioManager.getStreamVolume(3);
        getActivity().setVolumeControlStream(3);
        //  SeekBar localSeekBar = (SeekBar)findViewById(R.id.seekBar);
        SeekBar localSeekBar = (SeekBar) rootview.findViewById(R.id.seekBar);

        localSeekBar.setMax(i);
        localSeekBar.setProgress(j);
        localSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar paramAnonymousSeekBar, int paramAnonymousInt, boolean paramAnonymousBoolean) {
                audioManager.setStreamVolume(3, paramAnonymousInt, 0);
            }

            public void onStartTrackingTouch(SeekBar paramAnonymousSeekBar) {
            }

            public void onStopTrackingTouch(SeekBar paramAnonymousSeekBar) {
            }
        });
        return rootview;


    }

    private void setupIcyURLStreamHandler() {
        try {
            URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
                public URLStreamHandler createURLStreamHandler(String paramAnonymousString) {
                    Log.d("LOG", "Asking for stream handler for protocol: '" + paramAnonymousString + "'");
                    if ("icy".equals(paramAnonymousString)) ;
                    for (IcyURLStreamHandler localIcyURLStreamHandler = new IcyURLStreamHandler(); ; localIcyURLStreamHandler = null)
                        return localIcyURLStreamHandler;
                }
            });
            return;
        } catch (Throwable localThrowable) {
            while (true)
                Log.w("LOG", "Cannot set the ICY URLStreamHandler - maybe already set ? - " + localThrowable);
        }
    }

    public void startStreamingAudio() {
        try {
            if (this.audioStreamer != null)
                this.audioStreamer.interrupt();
            this.audioStreamer = new StreamingMediaPlayer(getActivity(), playButton);
            this.audioStreamer.startStreaming(urlStreaming, 5208L, 216L);
            return;

        } catch (IOException localIOException) {
            while (true)
                Log.e(getClass().getName(), "Error starting to stream audio.", localIOException);
        }
    }




    public void onDestroy()
    {
        super.onDestroy();
        if (this.audioStreamer != null)
            this.audioStreamer.interrupt();
    }



}
  • Behold here an example

No answers

Browser other questions tagged

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