音频控件重叠视图 - MusicBobber

bruce · 2016-04-29 16:42 · 223次阅读

满足音频控件重叠视图通过Cleveroad (https://github.com/Cleveroad/MusicBobber)

Setup and usage

使用的音频控件覆盖先加的依赖到您的项目:

dependencies {
compile 'com.cleveroad:audiowidget:0.9.1'
}

该库将增加两个新的权限,以你的清单:

<!-- used for drawing widget. This permission must be granted before calling AudioWidget.show(). -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<!-- used for notifing user that he is about to remove widget when he drags it on remove widget icon. -->
<!-- This permission granted by default on Android 6.0+ devices. -->
<uses-permission android:name="android.permission.VIBRATE"/>

然后,你可以创建一个使用部件制造商的新实例:

AudioWidget audioWidget = new AudioWidget.Builder(context)
    .lightColor(...)
    .darkColor(...)
    .expandWidgetColor(...)
    .progressColor(...)
    .progressStrokeWidth(...)
    .crossColor(...)
    .crossOverlappedColor(...)
    .crossStrokeWidth(...)
    .buttonPadding(...)
    .bubblesMinSize(...)
    .bubblesMaxSize(...)
    .shadowColor(...)
    .shadowRadius(...)
    .shadowDx(...)
    .shadowDy(...)
    .playDrawable(...)
    .pauseDrawable(...)
    .playlistDrawable(...)
    .prevTrackDrawale(...)
    .nextTrackDrawable(...)
    .defaultAlbumDrawable(...)
    .edgeOffsetXCollapsed(...)
    .edgeOffsetYCollapsed(...)
    .edgeOffsetXExpanded(...)
    .edgeOffsetYExpanded(...)
    .build();

或者你可以使用默认配置。只要致电:

AudioWidget audioWidget = new AudioWidget.Builder(context).build();

然后你可以使用音频部件控制器侦听事件:

// media buttons' click listener
audioWidget.controller().onControlsClickListener(new AudioWidget.OnControlsClickListener() {
   @Override
   public boolean onPlaylistClicked() {
         // playlist icon clicked
        // return true to collapse widget, false to stay in expanded state
}

  @Override
   public void onPreviousClicked() {
       // previous track button clicked
}

  @Override
   public boolean onPlayPauseClicked() {
      // return true to change playback state of widget and play button click animation (in collapsed state)
    return true;
}

  @Override
   public void onNextClicked() {
      // next track button clicked
}

  @Override
   public void onAlbumClicked() {
      // album cover clicked
}
});

// widget's state listener
audioWidget.controller().onWidgetStateChangedListener(new AudioWidget.OnWidgetStateChangedListener() {
   @Override
    public void onWidgetStateChanged(@NonNull AudioWidget.State state) {
      // widget state changed (COLLAPSED, EXPANDED, REMOVED)
}

   @Override
    public void onWidgetPositionChanged(int cx, int cy) {
      // widget position change. Save coordinates here to reuse them next time AudioWidget.show(int, int) called.
}
});

使用音频Widget.Controller,你可以得到跟踪持续时间,当前位置或专辑封面。您也可以设置使用当前的播放状态 start(), pause() or stop() methods. 看到 MusicService 类关于如何使用控制器的更多信息。

要在屏幕上显示通话的音频部件** AudioWidget.show(int, int)** 方法。 为了隐藏通话** AudioWidget.hide()** 方法。 很简单!

audioWidget.show(100, 100); // coordinates in pixels on screen from top left corner
...
audioWidget.hide();

但要确保你的应用程序有权在其他应用之上Android中6.0+。你可以像下面这样做(在活动):

...

// somewhere in your code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
}

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.canDrawOverlays(this)) {
            // now you can show audio widget
        }
    }
}

...

不错不错,有好久没弄安卓啦

登录后可以进行评论。没有账号?马上注册