Android视频播放时停止后台运行的方法

来源:互联网 发布:张海山锐线体简 mac 编辑:程序博客网 时间:2024/05/17 03:33
在项目中,遇到了视频播放,可是后台播放的音乐也同时播放,我们要的效果肯定是视频播放的时候,音乐暂停,视频播放完了我们就继续播放音乐,于是就找到了这个方法。
/**@param bMute 值为true时为关闭背景音乐。*/  @TargetApi(Build.VERSION_CODES.FROYO)  public static boolean muteAudioFocus(Context context, boolean bMute) {      if(context == null){          Log.d("ANDROID_LAB", "context is null.");          return false;      }      if(!VersionUtils.isrFroyo()){          // 2.1以下的版本不支持下面的API:requestAudioFocus和abandonAudioFocus          Log.d("ANDROID_LAB", "Android 2.1 and below can not stop music");          return false;      }      boolean bool = false;      AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);      if(bMute){          int result = am.requestAudioFocus(null,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);          bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;      }else{          int result = am.abandonAudioFocus(null);          bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;      }      Log.d("ANDROID_LAB", "pauseMusic bMute="+bMute +" result="+bool);      return bool;  }  
0 0
原创粉丝点击