0

The Emulator I am using is Galaxy Nexus API 22, and my background music(mp3) works perfectly on it and running as a service. However, it does not work on my SUMSUNG Galaxy Note, which has API 16, I don't think it is a API-related problem though.

Am i need to write some<uses-permission .../> in Manifest? or other solutions.

Thank you in advance!!

Here is the problem

Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
    ComponentName topActivity = taskInfo.get(0).topActivity;
    if (!topActivity.getPackageName().equals(context.getPackageName())) {
        SoundService.pauseSound();
        Toast.makeText(WelcomePage.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(WelcomePage.this, "YOU SWITCHED ACTIVITIES WITHIN YOUR APP", Toast.LENGTH_SHORT).show();
    }
}

I used these code in the onPause() in WelcomePage Activity (which is the 'MainActivity' in my App) in order to pause the background music when I press the HOME key or whatever.But I found the Toast sometimes immediately shows "YOU LEFT YOUR APP" when this Activity shows up without do anything, so I think these piece of code is where the problem is. Can anyone help me? Thank you!!

RobotCharlie
  • 1,180
  • 15
  • 19

2 Answers2

0

Try this code, its works here:

In activity, Declare the media player session before the oncreate like this;

MediaPlayer mp = new MediaPlayer();

Put the mp3 file on res/raw folder!

Finaly put this code on your java! (If you want this play automatic put on onCreate, or you can put this in a button if you want).

mp.stop(); // Stop this if run in another place
mp = MediaPlayer.create(this, R.raw.nameofyourfile);//Load your file from folder
mp.setLooping(false); // Set loop is off, if you want it's on change (true)
mp.start(); // start playing your sound

I hope to help you!

0

Whats the error, can you copy here the log. Maybe its a problem with the path to the mp3.

Gjoko Bozhinov
  • 1,137
  • 11
  • 15