
freelager
App-Anbieter (Beta)
- 4
Hallo Leute.
Ich habe mir ein examples von eine Internet Radio Veranstalter geholt.
https://github.com/speakingcode/anonplusradio-android
Ich habe meine Activity umgeschrieben.
Ich habe versuch eine zweiten stream rein zumachen aber ohne erfolg.
könnt ihr meine Activity bearbeiten das es funktioniert?
Das sind meine Activitys
MainActivity
das ist meine CONSTANTS
da möchte ich den zweiten stream rein bekommen.
das ist meine SteamStation Activity
und die letzte wo ich glaube wo was verändert werden muss.
StreamStationSpinnerAdapter
Danke im voraus
LG Freelager
Ich habe mir ein examples von eine Internet Radio Veranstalter geholt.
https://github.com/speakingcode/anonplusradio-android
Ich habe meine Activity umgeschrieben.
Ich habe versuch eine zweiten stream rein zumachen aber ohne erfolg.
könnt ihr meine Activity bearbeiten das es funktioniert?
Das sind meine Activitys
MainActivity
Code:
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ToggleButton;
import de.radiomega4u.android.R;
import de.radiomega4u.android.media.mediaplayer.IMediaPlayerServiceClient;
import de.radiomega4u.android.media.mediaplayer.MediaPlayerService;
import de.radiomega4u.android.media.mediaplayer.StatefulMediaPlayer;
import de.radiomega4u.android.media.mediaplayer.MediaPlayerService.MediaPlayerBinder;
import de.radiomega4u.audio.media.streamStation.StreamStation;
import de.radiomega4u.audio.media.streamStation.StreamStationSpinnerAdapter;
public class MainActivity
extends
Activity
implements
IMediaPlayerServiceClient
{
private StatefulMediaPlayer mMediaPlayer;
private StreamStation mSelectedStream = CONSTANTS.DEFAULT_STREAM_STATION;
private MediaPlayerService mService;
private boolean mBound;
WebView song;
private volatile WebViewClient mWebViewClient;
private ProgressDialog mProgressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bindToService();
mProgressDialog = new ProgressDialog(this);
initializeButtons();
setupStationPicker();
song = (WebView)findViewById(R.id.webView1);
song.getSettings().setJavaScriptEnabled(true);
song.setVerticalScrollBarEnabled(false);
song.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
song.loadUrl("file:///android_res/drawable/index.html");
}
});
song.setBackgroundColor(0x00000000);
song.loadUrl("http://radio-mega4u.de/webapp/song.php");
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.menuHistory:
showHistoryActivity();
return true;
case R.id.menuAbout1:
showAboutActivity();
return true;
case R.id.menuClose1:
shutdownActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Launches an activity to show the about
*/
private void showAboutActivity()
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(this, AboutActivity.class.getName());
startActivity(intent);
}
private void showHistoryActivity()
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(this, HistoryActivity.class.getName());
startActivity(intent);
}
/**
* Binds to the instance of MediaPlayerService. If no instance of MediaPlayerService exists, it first starts
* a new instance of the service.
*/
public void bindToService()
{
Intent intent = new Intent(this, MediaPlayerService.class);
if (mediaPlayerServiceRunning())
{
// Bind to Service
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
//no instance of service
else
{
//start service and bind to it
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
}
/**
* Sets up the stationPicker spinner
* Ich glaube hier muss man was verändern das es geht das man mehr adden kann.
*/
public void setupStationPicker()
{
Spinner stationPicker = (Spinner) findViewById(R.id.stationPicker);
StreamStationSpinnerAdapter adapter = new StreamStationSpinnerAdapter(
this, android.R.layout.simple_spinner_item);
//populate adapter with stations
for(StreamStation st : CONSTANTS.ALl_STATIONS)
{
adapter.add(st);
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
stationPicker.setAdapter(adapter);
stationPicker.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
StreamStation selectedStreamStation = (StreamStation) parent.getItemAtPosition(pos);
if (selectedStreamStation != mSelectedStream)
{
mService.stopMediaPlayer();
mSelectedStream = selectedStreamStation;
mService.initializePlayer(mSelectedStream);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing.
}
});
}
private void initializeButtons()
{
// PLAY/PAUSE BUTTON
final ToggleButton playPauseButton = (ToggleButton) findViewById(R.id.playPauseButton);
playPauseButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (mBound) {
mMediaPlayer = mService.getMediaPlayer();
//pressed pause ->pause
if (!playPauseButton.isChecked())
{
if (mMediaPlayer.isStarted())
{
mService.stopMediaPlayer();
}
}
//pressed play
else if (playPauseButton.isChecked())
{
// STOPPED, CREATED, EMPTY, -> initialize
if (mMediaPlayer.isStopped()
|| mMediaPlayer.isCreated()
|| mMediaPlayer.isEmpty())
{
mService.initializePlayer(mSelectedStream);
}
//prepared, paused -> resume play
else if (mMediaPlayer.isPrepared()
|| mMediaPlayer.isPaused())
{
mService.startMediaPlayer();
}
}
}
}
});
}
/**
* Defines callbacks for service binding, passed to bindService()
*/
private ServiceConnection mConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className, IBinder serviceBinder)
{
Log.d("MainActivity","service connected");
//bound with Service. get Service instance
MediaPlayerBinder binder = (MediaPlayerService.MediaPlayerBinder) serviceBinder;
mService = binder.getService();
//send this instance to the service, so it can make callbacks on this instance as a client
mService.setClient(MainActivity.this);
mBound = true;
//Set play/pause button to reflect state of the service's contained player
final ToggleButton playPauseButton = (ToggleButton) findViewById(R.id.playPauseButton);
playPauseButton.setChecked(mService.getMediaPlayer().isPlaying());
//Set station Picker to show currently set stream station
Spinner stationPicker = (Spinner) findViewById(R.id.stationPicker);
if(mService.getMediaPlayer() != null && mService.getMediaPlayer().getStreamStation() != null)
{
for (int i = 0; i < CONSTANTS.ALl_STATIONS.length; i++)
{
if (mService.getMediaPlayer().getStreamStation().equals(CONSTANTS.ALl_STATIONS[i]))
{
stationPicker.setSelection(i);
mSelectedStream = (StreamStation) stationPicker.getItemAtPosition(i);
}
}
}
}
@Override
public void onServiceDisconnected(ComponentName arg0)
{
mBound = false;
mService = null;
}
};
/** Determines if the MediaPlayerService is already running.
* @return true if the service is running, false otherwise.
*/
private boolean mediaPlayerServiceRunning()
{
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.anonplusradio.android.media.mediaplayer.MediaPlayerService".equals(service.service.getClassName()))
{
return true;
}
}
return false;
}
public void onInitializePlayerSuccess()
{
mProgressDialog.dismiss();
final ToggleButton playPauseButton = (ToggleButton) findViewById(R.id.playPauseButton);
playPauseButton.setChecked(true);
}
public void onInitializePlayerStart()
{
mProgressDialog = ProgressDialog.show(this, "Radio-Mega4u", "Connecting...", true);
mProgressDialog.getWindow().setGravity(Gravity.TOP);
mProgressDialog.setCancelable(true);
mProgressDialog.setOnCancelListener(new OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialogInterface)
{
MainActivity.this.mService.resetMediaPlaer();
final ToggleButton playPauseButton = (ToggleButton) findViewById(R.id.playPauseButton);
playPauseButton.setChecked(false);
}
});
}
@Override
public void onError() {
mProgressDialog.cancel();
}
/**
* Closes unbinds from service, stops the service, and calls finish()
*/
public void shutdownActivity() {
if (mBound) {
mMediaPlayer = mService.getMediaPlayer();
if (mMediaPlayer.isStarted())
{
mService.stopMediaPlayer();
Intent intent = new Intent(this, MediaPlayerService.class);
stopService(intent);
}
// Detach existing connection.
unbindService(mConnection);
mBound = false;
}
finish();
}
@Override
public void onDestroy()
{
super.onDestroy();
if (mBound)
{
mService.unRegister();
unbindService(mConnection);
mBound = false;
}
}
}
das ist meine CONSTANTS
da möchte ich den zweiten stream rein bekommen.
Code:
import de.radiomega4u.audio.media.streamStation.StreamStation;
public class CONSTANTS {
public static final String SITE_URL = "http://site.anonplusradio.com";
public static final String STREAM_1_URL = "http://88.198.143.54:10102/";
public static final String STREAM_1_LABEL = "RadioMega4u";
public static final String STREAM_2_URL = "http://88.198.143.54:10104/";
public static final String STREAM_2_LABEL = "RadioMega4u 32Kb/s";
public static final StreamStation[] ALl_STATIONS =
{
new StreamStation(STREAM_1_LABEL, STREAM_1_URL)
};
public static final StreamStation DEFAULT_STREAM_STATION = ALl_STATIONS[0];
}
das ist meine SteamStation Activity
Code:
/**
*
*/
package de.radiomega4u.audio.media.streamStation;
public class StreamStation {
private String mStationLabel;
private String mStationUrl;
/**
* Constructs a new StreamStation object with empty label and URL
*/
public StreamStation() {
this("", "");
}
/**
* Constructs a new StreamStation object with specified label and URL
* @param stationLabel
* @param stationUrl
*/
public StreamStation(String stationLabel, String stationUrl) {
mStationLabel = stationLabel;
mStationUrl = stationUrl;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StreamStation other = (StreamStation) obj;
if (mStationLabel == null) {
if (other.mStationLabel != null)
return false;
}
else if (!mStationLabel.equals(other.mStationLabel))
return false;
if (mStationUrl == null) {
if (other.mStationUrl != null)
return false;
}
else if (!mStationUrl.equals(other.mStationUrl))
return false;
return true;
}
/**
* Gets the station's label as a String
* @return the station label
*/
public String getStationLabel() {
return mStationLabel;
}
/**
* Gets the station's URL, as a String
* @return the URL of the station
*/
public String getStationUrl() {
return mStationUrl;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((mStationLabel == null) ? 0 : mStationLabel.hashCode());
result = prime * result
+ ((mStationUrl == null) ? 0 : mStationUrl.hashCode());
return result;
}
/**
* Sets a String as the station's label
* @param stationLabel the label to set
*/
public void setStationLabel(String stationLabel) {
this.mStationLabel = stationLabel;
}
/**
* Set's a String as the station's URL
* @param stationUrl the URL of the Station
*/
public void setStationUrl(String mStationUrl) {
this.mStationUrl = mStationUrl;
}
}
und die letzte wo ich glaube wo was verändert werden muss.
StreamStationSpinnerAdapter
Code:
package de.radiomega4u.audio.media.streamStation;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
*
*/
public class StreamStationSpinnerAdapter extends ArrayAdapter<StreamStation> {
int mTextViewResourceId;
Context mContext;
/**
* @param context
* @param textViewResourceId
* @param objects
*/
public StreamStationSpinnerAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.mTextViewResourceId = textViewResourceId;
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView)(super.getView(position, convertView, parent));
tv.setText(getItem(position).getStationLabel());
return tv;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView)(super.getDropDownView(position, convertView, parent));
tv.setText(getItem(position).getStationLabel());
return tv;
}
}
Danke im voraus
LG Freelager