package io.r_a_d.radio;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

import java.io.IOException;

public class ActivityMain extends AppCompatActivity {

    private MediaPlayer mediaPlayer = null;
    private boolean playing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new CustomPagerAdapter(this));
        viewPager.setOffscreenPageLimit(3);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(this, Uri.parse("https://stream.r-a-d.io/main.mp3"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }

    public void togglePlayPause(View v) throws IOException {
        ImageButton img = (ImageButton)v.findViewById(R.id.play_pause);
        if(!playing){
            img.setImageResource(R.drawable.pause_small);
            mediaPlayer.prepareAsync();
            playing = true;
        } else {
            img.setImageResource(R.drawable.arrow_small);
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(this, Uri.parse("https://stream.r-a-d.io/main.mp3"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
            playing = false;
        }
    }
}

@@ -432,11 +432,24 @@
  = n
-ull
+ew MediaPlayer()
 ;%0D%0A 
@@ -484,16 +484,84 @@
  false;%0D
+%0A    private String radio_url = %22https://stream.r-a-d.io/main.mp3%22;%0D
 %0A%0D%0A    @
@@ -904,36 +904,215 @@
 );%0D%0A
-%0D%0A        mediaPlayer = new 
+        setupMediaPlayer();%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    protected void onDestroy() %7B%0D%0A        super.onDestroy();%0D%0A        mediaPlayer.reset();%0D%0A        mediaPlayer.release();%0D%0A    %7D%0D%0A%0D%0A    public void setup
 Medi
@@ -1116,25 +1116,26 @@
 ediaPlayer()
-;
+ %7B
 %0D%0A        me
@@ -1249,59 +1249,17 @@
 rce(
-this, Uri.parse(%22https://stream.r-a-d.io/main.mp3%22)
+radio_url
 );%0D%0A
@@ -1567,210 +1567,8 @@
 %0D%0A%0D%0A
-    @Override%0D%0A    protected void onDestroy() %7B%0D%0A        super.onDestroy();%0D%0A        if (mediaPlayer.isPlaying()) %7B%0D%0A            mediaPlayer.stop();%0D%0A        %7D%0D%0A        mediaPlayer.release();%0D%0A    %7D%0D%0A%0D%0A
     
@@ -1931,41 +1931,8 @@
 );%0D%0A
-            mediaPlayer.stop();%0D%0A
     
@@ -1977,512 +1977,25 @@
     
-mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);%0D%0A            try %7B%0D%0A                mediaPlayer.setDataSource(this, Uri.parse(%22https://stream.r-a-d.io/main.mp3%22));%0D%0A            %7D catch (IOException e) %7B%0D%0A                e.printStackTrace();%0D%0A            %7D%0D%0A            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() %7B%0D%0A                @Override%0D%0A                public void onPrepared(MediaPlayer mp) %7B%0D%0A                    mp.start();%0D%0A                %7D%0D%0A            %7D
+setupMediaPlayer(
 );%0D%0A

Clean up streaming, get rid of warnings