0

Hi there im using http://code.google.com/p/aacdecoder-android/ heres my main activity full code: http://pastebin.com/FmaVgfNP but now i want to put that app in the background so when people press back button the app stays streaming audio i tried using this: basically the app have two activities one listview then when i click in one item it autoplays the stream for that station but when i press back button my streams stops, i tried to create a service like this:

public class MyService extends Service {
    private static final String TAG = "AACPlayerActivity";


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");

        //player = MediaPlayer.create(this, R.raw.braincandy);
        //player.setLooping(false); // Set looping
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        //player.stop();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
        //player.start();
    }
}

i tried this code to configure service in my activity:

startService(new Intent(this, MyService.class));

in my main activity when aacplayer starts

then when stops i put this:

stopService(new Intent(this, MyService.class));

i tried to put in MyService.class oncreate the function multiPlayer.playAsync( getUrl()); so the audio stream keeps streaming when people press back button.

but im getting this error:

10-16 02:02:07.760: E/AndroidRuntime(25638): java.lang.RuntimeException: Unable to start service com.spoledge.aacplayer.MyService@41b40ca0 with Intent { cmp=com.spoledge.aacplayer/.MyService }: java.lang.NullPointerException

i dont know if im doing right with the logic im not very clear about when my player streams the audio i can register that action as a service so it keeps in background.

thanks very much.

alexistkd
  • 906
  • 2
  • 14
  • 34

1 Answers1

1

Ive done this before using a bound service. Bound is better as it allows you to easily implement pause/stop/play, etc. If your activity is killed by the system while in the background, it can re-bind to the service when the user brings it back up, which re-enabled control of playback.

I'm not sure if it is bad practice to post links to my own blog but the full source code is here at http://www.speakingcode.com/2012/02/22/creating-a-streaming-audio-app-for-android-with-android-media-mediaplayer-android-media-audiomanager-and-android-app-service/

If linking is not preferred I'll duplicate the solution here, but it is lengthy :-) good luck and happy coding!

If you want to use a started service like yuo are doing, and forfeit the benefits of a bound service, then you need to move your MediaPlayer object entirely into the service and out of the activity. you can instantiate the MediaPlayer in your onCreate() but it will be better to start it (call playAsync()) in the onStartCommand() method.

speakingcode
  • 1,518
  • 13
  • 12
  • there isnt a shorter way bro? i mean if u take a look what i did is not too long maybe that can work also? shorter? – alexistkd Oct 16 '12 at 15:07