OnePlus Buds - Autoplay Last Played Music App 1.0

Playback the music automatically with the last used music app

Tags:
  1. autormali
    Once you put your buds in the ears the music is played automatically with last used music player. In-ear detection autoplayback is based on logcat entries so read system logs permission need to be grated to Tasker. Tasker is reading logcat entries generated by OnePlus Buds app - you need to have this app installed as well.

    In the Set Last Used Music App profile settings under Package set package name or slash / separated package names of the apps you want this profile to be triggered by. By default Spotify, Poweramp and YouTube Music are set:
    Code (Text):
    com.spotify.music/com.maxmpz.audioplayer/com.google.android.apps.youtube.music
    Modify it accordingly to set your music player(s).

    In the Earbuds BT Connected and Earbuds Low Battery Warning profiles the buds bluetooth name is used - by default OnePlus Buds. If you changed the name to different one, you should enter the name of your buds in those profiles options (leaving regex symbols/text sorounding the buds name unchanged).

    Autoplayback is working after bluetooth connection between the buds and the phone is established so don't be suprised it is not working when you put your buds in the ears before that fact, too early. Simply take one the earbud out and put it back in your ear to fix it and start playback.

    Follow set up task description as well as tasks labels for more info.

    Descriptions:

    Code (Text):

        Earbuds Setup Task (1059)
        A1: Ask Permissions [ Required Permissions:android.permission.READ_LOGS ]
        A2: Text Dialog [  Title:OP Buds Autoplay Last Played Music App Setup Text:In the <b>Set Last Used Music App</b> profile settings under <b>Package</b> set package name or <b>slash "/"</b> separated package names of the apps you want this profile to be triggered by.<br>By default <b>Spotify, Poweramp and YouTube Music</b>  are set: <b>com.spotify.music/com.maxmpz.audioplayer/com.google.android.apps.youtube.music</b><br>Modify it accordingly to set your music player(s).<br><br>In the <b>Earbuds BT Connected</b> and <b>Earbuds Low Battery Warning</b> profiles buds bluetooth name is used - by default <b>OnePlus Buds</b>. If you changed the name to different one, you should enter the name of your buds in those profiles options (leaving regex symbols/text sorounding the buds name unchanged). Button 1:Continue Button 2: Button 3: Close After (Seconds):120 Use HTML:On ]
        A3: Flash [ Text:Setup copleted if permission READ_LOGS granted Long:Off ]
     
    Code (Text):

        Play Last Music App (1212)
        Abort Existing Task
        <??>
        A1: Media Control [ Cmd:Play [Simulated Only] Simulate Media Button:On Package/App Name:%LastMusicApp Use Notification If Available:Off ]
     
    Code (Text):

        Profile: Set Last Used Music App (72)
        Event: Music Track Changed [ Output Variables:* Track:* Album:* Artist:* Package:com.spotify.music/com.maxmpz.audioplayer/com.google.android.apps.youtube.music Type:Playing ]
        Enter: Anon (928)
        <!?In the profile settings under <b>Package</b> set package name <br>or <b>slash "/"</b> separated package names of the apps you want this profile to be triggered by.<br>By default <b>Spotify, Poweramp and YouTube Music</b>  are set: <b>com.spotify.music/com.maxmpz.audioplayer/com.google.android.apps.youtube.music</b><br>Modify it accordingly.>
        A1: Anchor
        A2: Variable Set [ Name:%LastMusicApp To:%mt_app Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 Structure Output:Off ]
     
    Code (Text):

        Profile: Earbuds BT Connected (482)
        Priority: 20 Restore: no
        State: BT Connected [ Name:*OnePlus Buds* Address:* ]
        Enter: Anon (547)
        <!?In the profile settings under <b>Name</b> set your buds name. Default name is OnePlus Buds. If you changed the name you should enter the name of your buds (leaving regex symbols/text sorounding the buds name unchanged).>
        A1: Anchor
        <Profile status is set to on>
        A2: Profile Status [ Name:Earbud Left/Right In/Out LC Set:On ]
     
        Exit: Anon (549)
        <Profile status is set to off when buds are disconnected from Bluetooth to preserve phone battery. It is not necessary to monitor Logcat when buds are not used.>
        A1: Profile Status [ Name:Earbud Left/Right In/Out LC Set:Off ]
        <Clear variables in case setting them on during putting buds to the case>
        A2: Variable Clear [ Name:%EarbudLeft/%EarbudRight Pattern Matching:On Local Variables Only:Off Clear All Variables:Off ]
     
    Code (Text):

        Profile: Earbud Left/Right In/Out LC (722)
        Priority: 19 Restore: no Notification: no
        Event: Logcat Entry [ Output Variables:* Component:OnePlusPods Filter:-E 'String - info Device type = (1|2), in box = true, in ear = (false|true)' Grep Filter (Check Help):On ]
        Enter: Anon (724)
        Run Both Together
        <Earbud left in>
        A1: Variable Set [ Name:%EarbudLeft To:1 Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 Structure Output:Off ] If [ %lc_text ~R .*type = 1.* & %lc_text ~R .*in ear = true ]
        <Earbud left out>
        A2: Variable Clear [ Name:%EarbudLeft Pattern Matching:Off Local Variables Only:Off Clear All Variables:Off ] If [ %lc_text ~R .*type = 1.* & %lc_text ~R .*in ear = false ]
        <Earbud right in>
        A3: Variable Set [ Name:%EarbudRight To:1 Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 Structure Output:Off ] If [ %lc_text ~R .*type = 2.* & %lc_text ~R .*in ear = true ]
        <Earbud right out>
        A4: Variable Clear [ Name:%EarbudRight Pattern Matching:Off Local Variables Only:Off Clear All Variables:Off ] If [ %lc_text ~R .*type = 2.* & %lc_text ~R .*in ear = false ]
     
    Code (Text):

        Profile: Earbuds Play Music (954)
        Priority: 18 Restore: no
        State: Variable Value  [ %EarbudLeft Set & %EarbudRight Set ]
        Enter: Earbuds Play Music (953)
        Abort Existing Task
        <?? Play>
        A1: Perform Task [ Name:Play Last Music App Priority:%priority Parameter 1 (%par1): Parameter 2 (%par2): Return Value Variable: Stop:Off Local Variable Passthrough:Off Limit Passthrough To: Reset Return Variable:Off Allow Overwrite Variables:Off ] If [ %playing ~ false ]
        A2: Wait [ MS:0 Seconds:1 Minutes:0 Hours:0 Days:0 ]
        <Is music playing?>
        A3: Java Function [ Return:manage Class Or Object:CONTEXT Function:getSystemService
        {AudioManager} (String) Param:audio Param: Param: Param: Param: Param: Param: ]
        <%playing returns true or false>
        A4: Java Function [ Return:%playing Class Or Object:manage Function:isMusicActive
        {boolean} () Param: Param: Param: Param: Param: Param: Param: ]
        A5: Stop [ With Error:Off Task: ] If [ %playing ~ true ]
        <Loop up to get music playing (10 seconds)>
        A6: Goto [ Type:Action Label Number:1 Label:?? Play ] If [ %playing ~ false & %qtime < 10 ]
        A7: Say [ Text:Something went wrong.
        Can't play music.
        Take one the earbud out and put it back in your ear. Engine:Voice:com.google.android.tts:eng-usa Stream:3 Pitch:5 Speed:5 Respect Audio Focus:On Network:Off Continue Task Immediately:Off ]
     
    Code (Text):

        Profile: Earbuds Low Battery Warning (223)
        Priority: 14 Restore: no
        Event: Notification [ Owner Application:* Title:~R.*OnePlus Buds.*•.* Text:~R.*\s([1-9]|10)%.* Subtext:* Messages:* Other Text:* Cat:* New Only:Off ]
        Enter: Anon (227)
        <!?In the profile settings under <b>Name</b> set your buds name. Default name is OnePlus Buds. If you changed the name you should enter the name of your buds (leaving regex symbols/text sorounding the buds name unchanged).>
        A1: Anchor
        <Proceed after 15 seconds, after buds are in ears.>
        A2: Wait Until [ MS:0 Seconds:10 Minutes:0 Hours:0 Days:0 ] If [ %qtime > 15 ]
        <Stop. Want to hear warning only on buds, not a phone spiker.>
        A3: Stop [ With Error:Off Task: ] If [ %EarbudLeft Set & %EarbudRight Set ]
        <Match words " Low battery" in all languages>
        A4: Variable Search Replace [ Variable:%evtprm2 Search:.*•\s([^$]*) Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In Array: Replace Matches:On Replace With:$1 ]
        <New line. To give gap or pause between words>
        A5: Variable Set [ Name:%ret To:
         Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 Structure Output:Off ]
        A6: Variable Search Replace [ Variable:%evtprm3 Search:% Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In Array: Replace Matches:On Replace With:%%ret ]
        <Say low battery warning>
        A7: Say [ Text:%evtprm2 %ret %evtprm3 Engine:Voice:default:default Stream:3 Pitch:5 Speed:5 Respect Audio Focus:On Network:Off Continue Task Immediately:Off ]