I want to use for when changing Fragment continue to run the audio and when it returns to class! isPlaying continue to appear the requested pause. How do I do this with the 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();
}
}