播放声音窗口手机7模拟器时发出噪音

时间:2012-02-16 09:09:16

标签: windows-phone-7 audio

我有一个.wav音乐文件。 当我尝试使用SoundEffect.Play();

播放时

mus1.Play();我得到的是噪音而不是音乐。 这同样适用于PC音乐播放器。 如何解决这个问题?

如何确保它在Windows Phone 7中也能发挥出色?

3 个答案:

答案 0 :(得分:0)

尝试以下代码

MainPage.xaml.cs中

namespace MediaSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            progress.DataContext = this;
            this.Loaded += new System.Windows.RoutedEventHandler(MainPage_Loaded);
            mediaplayer.BufferingProgressChanged += new System.Windows.RoutedEventHandler(mediaplayer_BufferingProgressChanged);
            mediaplayer.CurrentStateChanged += new RoutedEventHandler(mediaplayer_CurrentStateChanged);
        }

        public double Duration { get; set; }

        void mediaplayer_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            if (mediaplayer.CurrentState == MediaElementState.Playing)
            {
                Duration = mediaplayer.NaturalDuration.TimeSpan.TotalSeconds;
                ThreadPool.QueueUserWorkItem(o =>
                {
                    while (true)
                    {
                        Dispatcher.BeginInvoke(new Action(() => Progress = mediaplayer.Position.TotalSeconds * 100 / Duration));
                        Thread.Sleep(0);
                    }
                });
            }
        }

        void mediaplayer_BufferingProgressChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            MediaElement mediaplayer = sender as MediaElement;
            if (mediaplayer.BufferingProgress == 1)
                Debug.WriteLine(mediaplayer.NaturalDuration);
        }

        void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            mediaplayer.Source = new Uri("/Oru nalum..mp3", UriKind.Relative);
            mediaplayer.Play();
        }



        public double Progress
        {
            get { return (double)GetValue(ProgressProperty); }
            set { SetValue(ProgressProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Progress.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ProgressProperty =
            DependencyProperty.Register("Progress", typeof(double), typeof(PhoneApplicationPage), new PropertyMetadata(0.0));



    }
}

MainPage.xaml中

<phone:PhoneApplicationPage 
    x:Class="MediaSample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Media Application" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>        

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <MediaElement x:Name="mediaplayer"/>
            <ProgressBar Height="40"  x:Name="progress" Value="{Binding Path=Progress}"/>
        </Grid>
    </Grid>


</phone:PhoneApplicationPage>

添加Mp3文件如下, enter image description here

就像你可以添加.wav文件一样。工作得很好。上面测试过的代码。 谢谢。

答案 1 :(得分:0)

使用MeadiaElement而不是使用SoundEffect类。 试试这个 MediaElement

答案 2 :(得分:0)

听起来好像.wav文件中的内容是WP7无法理解的。为什么不尝试将其转换为.wav(是的,我知道它已经在.wav中),因为这可能会删除未知数据。

Audacity(http://audacity.sourceforge.net/)是免费的,将为您完成此操作。只需将.wav文件拖入其中,然后单击文件 - &gt;出口...