【问题标题】:Playing music in Powershell stops when assigned to button分配给按钮时,在 Powershell 中播放音乐停止
【发布时间】:2020-07-21 20:50:57
【问题描述】:

我遇到了一个问题,我无法通过从 Powershell 播放音乐来解决或理解,我在我的 Powershell 脚本之上制作了一个 WPF GUI。 一切都很完美,只是当我按下播放音乐按钮时,音乐开始播放,但几秒钟后停止。

或者将鼠标移到 WPF GUI 上时,音乐停止,我无法解决。当我在项目中抛出用于播放音乐的代码时,它可以完美运行,只有当我为其分配一个按钮时,问题才会开始。

所以我用一个简单的旧表单和一个按钮制作了一个精简版本,制作了一个 add_Click 事件以将按钮连接到代码并再次测试。同样的问题,音乐在几秒钟后或当您将鼠标移到表单上时停止播放。

现在我仍然有一台旧的 Windows 7 机器,上面还挂着 Powershell V2,猜猜它是如何完美运行的!然后我在那台机器上将 Powershell v2 升级到 V5,我遇到了与 Win 10(1909 和 PS 5.1)笔记本电脑相同的问题,所以在 V2 和 V2 之间的 Powershell 发生了一些变化,导致了这种行为,但我找不到什么。

一些例子,当我在项目中抛出这些代码行时,它可以工作:

    Add-Type -AssemblyName presentationcore
    $location = (C:\users\myuserid\test.mp3)
    $PlaySound = New-Object System.Windows.Media.MediaPlayer
    $PlaySound.open($location)
    $PlaySound.Play()

但是一旦我为其分配了一个按钮,就会出现上述问题

所以尽可能地排除一切:

Add-Type -AssemblyName System.Windows.Forms    
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName presentationcore

    # Build Form
    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "My Form"
    $Form.Size = New-Object System.Drawing.Size(200,200)
    $Form.StartPosition = "CenterScreen"
    $Form.Topmost = $True

    # Add Button
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(35,35)
    $Button.Size = New-Object System.Drawing.Size(120,23)
    $Button.Text = "Play music"

    $Form.Controls.Add($Button)

    #Add Button event 
    $Button.Add_Click({
          
        
        $location = 'D:\test\test.mp3'
        $PlaySound = New-Object System.Windows.Media.MediaPlayer
        $PlaySound.open($location)
        $PlaySound.Play()
        
    })
     
    #Show the Form 
    $form.ShowDialog()| Out-Null 

因此,在播放音乐时调整表单大小会导致 95% 的时间停止。但是当我在没有像这样的按钮的情况下将代码用于播放音乐时,它永远不会中断。

Add-Type -AssemblyName System.Windows.Forms    
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName presentationcore

    # Build Form
    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "My Form"
    $Form.Size = New-Object System.Drawing.Size(200,200)
    $Form.StartPosition = "CenterScreen"
    $Form.Topmost = $True

    # Add Button
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(35,35)
    $Button.Size = New-Object System.Drawing.Size(120,23)
    $Button.Text = "Play music"

    $Form.Controls.Add($Button)

    #Add Button event 
    $Button.Add_Click({
          
        
        #Button now does nothing.. and music plays without breaking...ever
        
    })
        
        #Now it will always play to the end no matter what :-S
        $location = 'D:\test\test.mp3'
        $PlaySound = New-Object System.Windows.Media.MediaPlayer
        $PlaySound.open($location)
        $PlaySound.Play()

     
    #Show the Form 
    $form.ShowDialog()| Out-Null 

【问题讨论】:

    标签: wpf winforms user-interface playback powershell-5.0


    【解决方案1】:

    (代表问题作者发布解决方案,将其移至答案空间)

    我自己解决了这个问题,诀窍是在脚本开头加载播放器,如下所示:

    #Clear the Console
    CLS
    
    #Determine Script location
    $SCRIPT_PARENT = Split-Path -Parent $MyInvocation.MyCommand.Definition
    
    #Add in the presentation core
    Add-Type -AssemblyName presentationframework, presentationcore
    
    #Load music player and set location here!
    $location = ($SCRIPT_PARENT + "\Music.mp3")
    $PlaySound = New-Object System.Windows.Media.MediaPlayer
    
    ###############################################################
    #  here comes a whole lot of code (XAML for WPF GUI etc etc)  #
    ###############################################################
    
    # Then in your event system only put:
    
    #Play button action 
    $MainGUI.Playmusic.add_Click({
         
         #Open file and play music
         $PlaySound.open($location)
         $PlaySound.Play()
    
    })
    

    这100%解决了播放问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多