florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
752 stars 357 forks source link

[ New Feature ] Custom notif Android/ios #243

Open florent37 opened 4 years ago

florent37 commented 4 years ago

Same interractions on Android & ios :

settings: NotificationSettings.multiplatform(
  prev: NotificationActionsPrev.prev(),
  playPause: null, //to disable it
  next: NotificationActionsNext.next(
      customAction: (player) {
           //action
      }
  ),
  stop: NotificationActions.stop(),
)
florent37 commented 4 years ago

Custom notif on platforms

settings: NotificationSettings.perPlatform(
    android: AndroidNotification(

        // because you can have up to 4 actions

        action1: NotificationActionsPrev.prev()
        action2:  NotificationActions.playPause(),
        action3:  NotificationActionsNext.next(),
        action4:  NotificationActions.stop(),
    ),
    ios: iOSNotification(
          prev: NotificationActionsPrev.prev()
          playPause: null, //to disable it
          next: NotificationActionsNext.next(),
          stop: NotificationActions.stop(),
    )
)
florent37 commented 4 years ago

Custom notif on platforms

settings: NotificationSettings.perPlatform(
    android: AndroidNotification(
        action1: NotificationActions.playPause(
              customAction: (player) {
                        //action
              }
        ),
    ),
    ios: iOSNotification(
          playPause: NotificationActions.playPause(
                  customAction: (player) {
                        //action
                 }
          ),
    )
)
florent37 commented 4 years ago

Custom action & custom icon Android

eg: custom icon (heart)

settings: NotificationSettings.perPlatform(
    android: AndroidNotification(

        action1:  NotificationActions.custom(
          iconBuilder: (player, currentAudio) {
            if(isFavorite(currentAudio.path)){
              return AndroidDrawable("ic_fav_full");
            } else {
              return AndroidDrawable("ic_fav_empty");
            }
          }
          customAction: (player, currentAudio, notification) {
            //action
            toggleIsFavorite(currentAudio.path);
            notification.updateIcon(); //will call iconBuilder
          }
        ),

    )
)
florent37 commented 4 years ago

seek instead of prev action

(on android, I need to find how to handle the icon)

settings: NotificationSettings.perPlatform(
    android: AndroidNotification(
        //eg: seek in place of prev
        action1:  NotificationActionsPrev.seek(by: Duration(seconds: 10)),

    ),
    ios: iOSNotification(
          //eg: seek in place of prev
          prev: NotificationActionsPrev.seek(by: Duration(seconds: 10)),
    )
)
chevalierv commented 4 years ago

I don't know if you already find out this sample code by Apple but I think I could be helping for this issue: https://developer.apple.com/documentation/mediaplayer/becoming_a_now_playable_app

It list all command possible with a control center as a sample app for iPhone. Really useful to test and see what's possible and what's not

florent37 commented 4 years ago

oh

NowPlayableCommand

case pause, play, stop, togglePausePlay
case nextTrack, previousTrack, changeRepeatMode, changeShuffleMode
case changePlaybackRate, seekBackward, seekForward, skipBackward, skipForward, changePlaybackPosition
case rating, like, dislike
case bookmark
case enableLanguageOption, disableLanguageOption
florent37 commented 4 years ago

https://stackoverflow.com/questions/20591156/is-there-a-public-way-to-force-mpnowplayinginfocenter-to-show-podcast-controls

For seek :

import MediaPlayer

let rcc = MPRemoteCommandCenter.shared()

let skipBackwardCommand = rcc.skipBackwardCommand
skipBackwardCommand.isEnabled = true
skipBackwardCommand.addTarget(handler: skipBackward)
skipBackwardCommand.preferredIntervals = [42]

let skipForwardCommand = rcc.skipForwardCommand
skipForwardCommand.isEnabled = true
skipForwardCommand.addTarget(handler: skipForward)
skipForwardCommand.preferredIntervals = [42]

func skipBackward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
    guard let command = event.command as? MPSkipIntervalCommand else {
        return .noSuchContent
    }

    let interval = command.preferredIntervals[0]

    print(interval) //Output: 42

    return .success
}

func skipForward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
    guard let command = event.command as? MPSkipIntervalCommand else {
        return .noSuchContent
    }

    let interval = command.preferredIntervals[0]

    print(interval) //Output: 42

    return .success
}
colinricardo commented 3 years ago

was this merged?

swifthing commented 2 years ago

was this merged?

same question, it would be awesome to custom iOS notifications :)

tudosxxx commented 10 months ago

The latest version still doesn’t work.