Android Open Source - AntenneE Main Activity






From Project

Back to project page AntenneE.

License

The source code is released under:

MIT License

If you think the Android project AntenneE listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.bzyx.antennee;
/*from  w  ww. j av  a  2  s .  co  m*/
import android.content.Context;
import android.media.MediaPlayer;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.os.PowerManager;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.net.Uri;
import android.widget.Chronometer;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
    private final String APP_TAG = "Antennee";
    private MediaPlayer mediaPlayer;
    private Chronometer stopWatch;
    private WifiManager.WifiLock wifiLock;
    private TextView timeText;
    private String theStream;
    private long startTime;
    private long countUp;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /* This is propably not so clever idea, making all things in one thread/function */
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");

        Toast.makeText(MainActivity.this,
                "Your music will be here for a moment!", Toast.LENGTH_LONG).show();

        final TextView valueTextView= (TextView) findViewById(R.id.textView);
        final Spinner s = (Spinner)findViewById(R.id.spinner);
        final MyData items[] = new MyData[5];
        items[0] = new MyData( "Antenne1","http://stream.antenne1.de/stream2/livestream.mp3" );
        items[1] = new MyData( "Big.fm","http://217.151.152.245/bigfm-mp3-64" );
        items[2] = new MyData( "RmfMaxx", "http://217.74.72.11:8000/rmf_maxxx");
        items[3] = new MyData( "RMF FM", "http://195.150.20.244:8000/rmf_fm");
        items[4] = new MyData( "Radio90", "http://streams.radio90.pl:8000/radio90_96kbps_stereo.mp3");

        ArrayAdapter<MyData> adapter = new ArrayAdapter<MyData>(this,android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
        s.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            MyData d = items[position];
                            valueTextView.setText( d.getValue());
                            theStream = d.getValue();
                        }
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                }
        );

        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer!=null && mediaPlayer.isPlaying()) {
                    /* Toast don't show up */
                    Toast.makeText(MainActivity.this, "Already playing!", Toast.LENGTH_SHORT);
                } else {
                    mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(theStream));
                    mediaPlayer.start();
                    mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
                    wifiLock.acquire();
                    stopWatch.setBase(SystemClock.elapsedRealtime());
                    stopWatch.start();
                }
            }
        });

        final Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer!=null && !mediaPlayer.isPlaying()){
                    /* Toast don't show up */
                    Toast.makeText(MainActivity.this, "Already stoped.", Toast.LENGTH_SHORT);
                } else {
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                    mediaPlayer.release();
                    stopWatch.stop();
                    mediaPlayer = null;
                    wifiLock.release();
                }
            }
        });

        stopWatch = (Chronometer) findViewById(R.id.chronometer);
        startTime = SystemClock.elapsedRealtime();

        /* This should be on center, don't know how to do that :( */
        timeText = (TextView) findViewById(R.id.textView2);
        stopWatch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener(){
            @Override
            public void onChronometerTick(Chronometer arg0) {
                countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
                String asText = (countUp / 60) + ":" + (countUp % 60);
                timeText.setText(asText);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        wifiLock.release();
        mediaPlayer.release();
    }

    class MyData {
        public MyData( String spinnerText, String value ) {
            this.spinnerText = spinnerText;
            this.value = value;
        }

        public String getSpinnerText() {
            return spinnerText;
        }

        public String getValue() {
            return value;
        }

        public String toString() {
            return spinnerText;
        }

        String spinnerText;
        String value;
    }
}




Java Source Code List

pl.bzyx.antennee.BuildConfig.java
pl.bzyx.antennee.MainActivity.java