Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by viesc123 · Jan 30, 2020 at 12:19 PM · editormacosxplaymode

Stop MacOS system media playback when Unity enters Playmode

I'm asking this after a serious talk with our very charming audio artist: I like listening to music on Youtube or Spotify while working, but I also want to hear the game's audio when I play it in the Unity Editor (no more muting Unity!). It is somewhat annoying to always pause my music player when I start the play mode. I noticed on Mac, that the play/pause function key on the keyboard is able to always stop and continue my current music player, so I was wondering if there is a way that I can trigger the media key from within Unity, whenever I enter or leave the Editor Play Mode.

alt text

I'll keep experimenting with it when I find time, but would be very happy about any suggestions!

img-20200130-131305.jpg (34.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by viesc123 · May 01, 2020 at 10:44 AM

So, this has been bugging me and finally I had time to do some reading up and to create a little script that handles at least my specific case: pausing/playing Spotify on OSX. It's a static class that attaches to Unity's Editor state change callback and talks to Spotify via the OSX terminal bash (Unfortunately, I didn't find a way to access the media key itself). I added a menu command to enable the functionality. Here's the code for whomever it concerns ;)

 using UnityEngine;
 using UnityEditor;
 using System.Diagnostics;
 
 [InitializeOnLoad] // call this class' constructor on complie and reload
 public static class MusicPauser
 {
     // currently only works for Spotify on OSX
     const string osxCommand =
 @"
 # check if spotify is started, otherwise next command will start the process and freeze unity's thread
 if pgrep Spotify; then
     # use osascript to talk to spotify
     osascript -e 'tell application ""spotify"" to {0}';
 fi";
 
     const string play = "play";
     const string pause = "pause";
 
     // enabler stored in player prefs
     public static bool StopMedia
     {
         get
         {
             if (PlayerPrefs.HasKey(typeof(MusicPauser).ToString()))
             {
                 return PlayerPrefs.GetInt(typeof(MusicPauser).ToString()) == 1;
             }
 
             return false;
         }
 
         set
         {
             PlayerPrefs.SetInt(typeof(MusicPauser).ToString(), value ? 1 : 0);
         }
     }
 
     // menu command to enable class functionality
     [MenuItem("Tools/Various/Toggle Music On Playmode Change")]
     public static void ToggleMusicPauser()
     {
         StopMedia = !StopMedia;
         UnityEngine.Debug.Log(StopMedia ? "Stopping media enabled." : "Stopping media disabled");
     }
 
     // on reload and compile attach to the editor's playModeChange callback
     static MusicPauser()
     {
         EditorApplication.playModeStateChanged += OnPlaymodeChange;
     }
 
     // depending on state change, call play or pause command
     public static void OnPlaymodeChange(PlayModeStateChange state)
     {
         if (!StopMedia) return;
 
         //UnityEngine.Debug.Log(state);
 
         if (state == PlayModeStateChange.EnteredPlayMode)
         {
 #if UNITY_EDITOR_OSX
             UnixShellCommand(string.Format(osxCommand, pause));
 #elif UNITY_EDITOR_WIN
             UnityEngine.Debug.LogError("Music Pauser not implemented for windows.");
 #endif
         }
         else if (state == PlayModeStateChange.ExitingPlayMode)
         {
 #if UNITY_EDITOR_OSX
             UnixShellCommand(string.Format(osxCommand, play));
 #elif UNITY_EDITOR_WIN
             UnityEngine.Debug.LogError("Music Pauser not implemented for windows.");
 #endif
         }
     }
 
     // call the command for the OSX shell
     private static void UnixShellCommand(string command)
     {
         ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash");
         startInfo.WorkingDirectory = "/";
         startInfo.UseShellExecute = false;
         startInfo.RedirectStandardInput = true;
         startInfo.RedirectStandardOutput = true;
 
         Process process = new Process();
         process.StartInfo = startInfo;
         process.Start();
 
         process.StandardInput.WriteLine(command);
         process.StandardInput.WriteLine("exit");
 
         process.WaitForExit();
     }
 }

Would be thrilled if you improve on it! Let's together not mute our game's audio again! ;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

156 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity editor freezes on play if visual studio is not attached 0 Answers

Unity Editor Freezes Every Few Seconds 4 Answers

Why Play Mode is taking a long time on Pro? 1 Answer

Keep background thread alive in Editor while entering PlayMode 0 Answers

Why changing material shader at runtime also affect my asset on disk? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges