0

I have tried to get this working so that it plays in the background?? why won't it work it looks fine to me?? I am new to java/android development so hope someone can fix the issue and explain what I problem was so I can learn something Yes I got it to stream so far but once you exit the app the music stops playing For being new think im doing great must be since I have previous experience with PHP etc.. Thanks ;)

package com.beanie.samples.streaming;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import com.beanie.samples.streaming.R;
import com.beanie.samples.streaming.MyService;

import android.app.Activity;
import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

;



public class HomeActivity extends Activity implements OnClickListener {
      private static final String TAG = "MyServices";

    private final static String RADIO_STATION_URL = "http://195.154.237.162:8936/";

    private static final String START_STICKY = null;
      Button buttonPlay, buttonStopPlay;


        /** Called when the activity is first created.
         * Keep this here all the application will stop working */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            initializeUIElements();

            initializeMediaPlayer();

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            buttonPlay = (Button) findViewById(R.id.buttonPlay);
            buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);

            buttonPlay.setOnClickListener(this);
            buttonStopPlay.setOnClickListener(this);





        }





    private ProgressBar playSeekBar;





    private MediaPlayer player;

    private InputStream recordingStream;

    private RecorderThread recorderThread;

    private boolean isRecording = false;







    private void initializeUIElements() {

        playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
        playSeekBar.setMax(100);
        playSeekBar.setVisibility(View.INVISIBLE);

        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(this);

        buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
        buttonStopPlay.setEnabled(false);
        buttonStopPlay.setOnClickListener(this);


    }




    public void startPlaying() {
        buttonStopPlay.setEnabled(true);
        buttonPlay.setEnabled(false);

        playSeekBar.setVisibility(View.VISIBLE);

        player.prepareAsync();

        player.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                player.start();



            }
        });

    }





    public void onClick(View v) {
        if (v == buttonPlay) {

            Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onClick: starting srvice");
            startService(new Intent(this, MyService.class));



         startPlaying();
  player.setLooping(false); // Set looping












        } else if (v == buttonStopPlay) {

            Log.d(TAG, "onClick: stopping srvice");
            stopService(new Intent(this, MyService.class));
            stopPlaying();

        }
    }




    private void stopPlaying() {
        if (player.isPlaying()) {
            player.stop();
            player.release();
            initializeMediaPlayer();
        }

        buttonPlay.setEnabled(true);
        buttonStopPlay.setEnabled(false);
        playSeekBar.setVisibility(View.INVISIBLE);

        stopRecording();
    }

    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        try {
            player.setDataSource(RADIO_STATION_URL);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.stop();
        }
    }

    private void startRecording() {

        BufferedOutputStream writer = null;
        try {
            URL url = new URL(RADIO_STATION_URL);
            URLConnection connection = url.openConnection();
            final String FOLDER_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + File.separator + "Songs";

            File folder = new File(FOLDER_PATH);
            if (!folder.exists()) {
                folder.mkdir();
            }

            writer = new BufferedOutputStream(new FileOutputStream(new File(FOLDER_PATH
                    + File.separator + "sample.mp3")));
            recordingStream = connection.getInputStream();

            final int BUFFER_SIZE = 100;

            byte[] buffer = new byte[BUFFER_SIZE];

            while (recordingStream.read(buffer, 0, BUFFER_SIZE) != -1 && isRecording) {
                writer.write(buffer, 0, BUFFER_SIZE);
                writer.flush();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                recordingStream.close();
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void stopRecording() {

        try {
            isRecording = false;
            if (recordingStream != null) {
                recordingStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class RecorderThread extends Thread {
        @Override
        public void run() {
            isRecording = true;
            startRecording();
        }

    };
}
Jonas
  • 121,568
  • 97
  • 310
  • 388
user279297
  • 43
  • 1
  • 10
  • You need to use a Service: http://developer.android.com/guide/components/services.html – Skynet Feb 22 '14 at 13:31
  • Ok What would I use to archive this – user279297 Feb 22 '14 at 14:19
  • This: http://stackoverflow.com/questions/21461191/alarmmanager-fires-alarms-at-wrong-time/21461246#21461246 and this: http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App will help you – Skynet Feb 22 '14 at 14:32
  • Thanks! It's late here 1am so I check them around 8 in the morning my time Thanks again – user279297 Feb 22 '14 at 14:36

1 Answers1

0
@Override
protected void onPause() {
    super.onPause();
    if (player.isPlaying()) {
        player.stop();
    }
}

well, it is stopping because you're asking it to stop. When the activity goes to background it pauses.

You should run the stream from a service (that stays in the background without issues) and use a bound service to communicate between activity and service http://developer.android.com/guide/components/bound-services.html

Budius
  • 39,391
  • 16
  • 102
  • 144
  • can't believe I didn't see that lol anyway I try removing that line and see how that goes and I check out the bound method Thanks! – user279297 Feb 22 '14 at 14:24