-2

I have a widget that when pressed opens the activity. I do it like this:

Piece of code:

        try {
            Intent defineIntent2 = new Intent(context,
                    Setting.class);
            PendingIntent pendingIntent2 = PendingIntent
                    .getActivity(context, 0 /* no requestCode */,
                            defineIntent2, 0 /* no flags */);
            updateViews
                    .setOnClickPendingIntent(R.id.widget, pendingIntent2);
        } catch (Exception e) {
            Log.e(LOG_TAG, "", e);
        }
        return updateViews;
    }

I want that by pressing widget the music start. I tried to do to play in the activity. This works, but the problem is I need to close the activity "by hand".

There is another way of releasing music when pressed?

user1704195
  • 179
  • 1
  • 4
  • 16

1 Answers1

0

You want music to play as you open an Activity from a widget?

Try this:

    MediaPlayer player = new MediaPlayer();
    player = MediaPlayer.create(YourClass.this, R.raw.yoursoundfile);
    player.start();

    //Your intent code here to launch activity

Edit: You'll need to use a service. Example here:http://stackoverflow.com/questions/4298888/android-music-player-widget

Henry
  • 486
  • 1
  • 5
  • 16