android - 当应用程序进入后台时停止声音

时间:2015-02-03 00:47:21

标签: android performance android-activity android-mediaplayer

当应用程序进入后台时,我使用以下代码来停止在应用程序后台循环播放的音乐,它可以在模拟器上运行,而不是在手机上运行。即使我退出应用程序声音仍然播放,任何人都有解决方案吗?我非常感激

@Override
protected void onPause() {
super.onPause();
Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
  ComponentName topActivity = taskInfo.get(0).topActivity; 
  if (!topActivity.getPackageName().equals(context.getPackageName())) {
      MusicManager.getInstance().stopMusic();
    Toast.makeText(MainActivity.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
  }
}

更新:

MusicManager资料:

public class MusicManager {
MediaPlayer mp = null;
private static MusicManager refrence = null;

public static MusicManager getInstance(){
if(refrence==null){
    refrence = new MusicManager();
}
return refrence;
}
public void initializeMediaPlayer(Context context, int musicID){ 
mp = MediaPlayer.create(context, R.raw.rock);
 mp.setLooping(true);   
 try {
        mp.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void playMusic(){
mp.start();
}
public void stopMusic(){
    if (mp != null) {
    mp.stop();
    mp.release();
}

}
}

主:

MusicManager.getInstance().initializeMediaPlayer(this, R.raw.rock);
MusicManager.getInstance().playMusic();

2 个答案:

答案 0 :(得分:0)

您应该使用activitylifecyclecallbacks来检查应用是前景还是后台。它在api级别14中添加。 您可以使用以下代码

 @SuppressLint("NewApi")
 public class MYLifeCycleHandler implements ActivityLifecycleCallbacks 
 {
  private static int resumed;
  private static int paused;
  private static int started;
  private static int stopped;

  @Override
  public void onActivityCreated(Activity activity, Bundle savedInstanceState) 
  {
  }

  @Override
  public void onActivityDestroyed(Activity activity) 
  {
  }

  @Override
  public void onActivityResumed(Activity activity)
  {
    ++resumed;
  }

  @Override
  public void onActivityPaused(Activity activity)
  {
    ++paused;
  }   

  @Override
  public void onActivityStarted(Activity activity) {
    ++started;        
    if(started > stopped)
    {
    //start music player if not already running
    MusicManager.getInstance().playMusic();
    }
   }

  public boolean isApplicationInForeground() 
  {
    return resumed > paused;
  }
  @Override
  public void onActivityStopped(Activity activity) {
    ++stopped;
    //if application in background
    if(stopped == started)
    {
        //stop music player if running
       MusicManager.getInstance().stopMusic();
    }
   }

   @Override
   public void onActivitySaveInstanceState(Activity activity, Bundle outState)       
   {        
   }
  }    

您应该按以下方式注册回调

getApplication().registerActivityLifecycleCallbacks(new MPLifeCycleHandler());

理想情况下,在第一项活动的onCreate中注册

答案 1 :(得分:0)

这是代码

主:

public class MainActivity extends Activity{

ImageView startB;
Bitmap b = null;
int keyCode = 0;
boolean onKeyDown = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getApplication().registerActivityLifecycleCallbacks(new MYLifeCycleHandler(getApplication())); // New error:the constructor MYLifeCycleHandler is undefined
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    /* adapt the image to the size of the display */
    Display d = getWindowManager().getDefaultDisplay();
    Point p = new Point();
    d.getSize(p);
    b = Bitmap.createScaledBitmap(
            BitmapFactory.decodeResource(getResources(), R.drawable.screen1),//b
            p.x, p.y, true);

    setContentView(R.layout.activity_main);

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl1);
    rl.setBackgroundDrawable(new BitmapDrawable(getResources(), b));

    ImageView startB = (ImageView) findViewById(R.id.startbutton);
    MusicManager.getInstance().initializeMediaPlayer(this, R.raw.rock);
    MusicManager.getInstance().playMusic();

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        onKeyDown = true;
    }

     startB.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(getApplicationContext(), WorkoutPlace2.class);
                startActivity(myIntent);
                finish();
            }
        });
}
@Override
public void onBackPressed(){
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    android.os.Process.killProcess(android.os.Process.myPid());
}
public void onHomePressed(){
    if (onKeyDown = true){
        AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        manager.setStreamMute(AudioManager.STREAM_MUSIC, true);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
/*@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    return super.onKeyDown(keyCode, event);
}*/

@Override
protected void onPause() {
    super.onPause();
    Context context = getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    if (!taskInfo.isEmpty()) {
      ComponentName topActivity = taskInfo.get(0).topActivity;
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
          MusicManager.getInstance().stopMusic();
        Toast.makeText(MainActivity.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
        AudioManager manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        manager.setStreamMute(AudioManager.STREAM_MUSIC, true);
      }
    }
    if (b != null){
        b.recycle();
        b = null;
    }
}
@Override
protected void onResume(){
    super.onResume();
    MusicManager.getInstance().playMusic();
}
}

MYLifeCycleHadler:

正如您更新的帖子