MediaElement Windows Phone 7

发布于 2024-10-20 07:30:36 字数 474 浏览 2 评论 0 原文

我正在创建一个小应用程序来帮助我更好地了解如何在 WP7 设备上播放声音,但我在实际让声音从设备中发出时遇到了问题。

我有以下代码:

<MediaElement x:Name="note1" Source="test.mp3" AutoPlay="False" />

private void btn1_Click(object sender, RoutedEventArgs e)
{
    note1.Source = new Uri("test.mp3", UriKind.Relative);
    note1.Play();
}

其中 test.mp3 的构建操作是资源。

我不明白的是,当我在方法 btn1_Click 上添加断点并在 note1.Play() 处停止时,它实际上会播放 test.mp3,但在没有断点的情况下进行调试并单击按钮时,我什么也没听到。

有办法解决这个问题吗?

I'm creating a little app to help me better understand how to play sounds on WP7 devices but I'm having a problem actually getting the sound to come out of the device.

I have the following code:

<MediaElement x:Name="note1" Source="test.mp3" AutoPlay="False" />

private void btn1_Click(object sender, RoutedEventArgs e)
{
    note1.Source = new Uri("test.mp3", UriKind.Relative);
    note1.Play();
}

Where test.mp3's Build Action is a Resource.

The thing I don't understand is when I add a breakpoint on the method btn1_Click and I stop at note1.Play() it actually plays test.mp3 but when debug without breakpoints and click on the button I hear nothing.

Is there a way to fix this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心不设防 2024-10-27 07:30:36

您是否尝试过将 test.mp3 的构建操作设置为内容。

另外,您是否在识别手机并完成同步后关闭zune软件,并使用wp7connect工具进行连接。有关 wp7connect 工具的更多信息,请尝试此处
zune 锁定 wp7 设备上的所有媒体,您无法播放任何媒体,但媒体的状态将是“已结束”。
尝试设置媒体的以下事件 MediaFailed MediaOpenedMediaEndedDownloadProgressChangedCurrentStateChangedBufferingProgressChanged

Have you tried playing with test.mp3's Build Action set as content.

Also did you close zune software after it recognizes the phone and completes sync, and connect using wp7connect tool. for more info about wp7connect tool try here.
zune locks all media on wp7 device and you cant play any media, but the status of the media will be "ended".
try setting up media's following events MediaFailed MediaOpened,MediaEnded, DownloadProgressChanged, CurrentStateChanged and BufferingProgressChanged

醉城メ夜风 2024-10-27 07:30:36

另外,请确保您已将功能 ID_CAP_MEDALIB 添加到清单 (WMAppManifest.xml),这似乎是 MediaElement 所必需的(否则您将在 MediaFailed 处理程序中收到 AG_E_NETWORK_ERROR)。

Also, make sure you have add the capability ID_CAP_MEDIALIB to your manifest (WMAppManifest.xml), this seems to be required for MediaElement (otherwise you'll get AG_E_NETWORK_ERROR in your MediaFailed handler).

聽兲甴掵 2024-10-27 07:30:36

我不建议将 mediaElement 用于多个音频项目..它有奇怪的效果...使用类似以下内容:

Stream stream = TitleContainer.OpenStream(@"Audio/buzzer.wav");

        SoundEffect effect = SoundEffect.FromStream(stream);
        FrameworkDispatcher.Update();
        effect.Play();

使用 xna 框架 ....并确保有 WAV 文件。

i dont recommend mediaElement for more than one audio item ..it has weird effects ...use something like:

Stream stream = TitleContainer.OpenStream(@"Audio/buzzer.wav");

        SoundEffect effect = SoundEffect.FromStream(stream);
        FrameworkDispatcher.Update();
        effect.Play();

using the xna framework ....and make sure there WAV files.

黯然#的苍凉 2024-10-27 07:30:36

Uri 类型必须是相对或绝对。

private void btn1_Click(object sender, RoutedEventArgs e)
{
    note1.Source = new Uri("test.mp3", UriKind.RelativeOrAbsolute);
    note1.Play();
}

Uri kind must be RelativeOrAbsolute.

private void btn1_Click(object sender, RoutedEventArgs e)
{
    note1.Source = new Uri("test.mp3", UriKind.RelativeOrAbsolute);
    note1.Play();
}
安静被遗忘 2024-10-27 07:30:36

您需要确保 MediaElement 已打开,然后才能对其调用 .Play() - 您可以通过向 MediaOpened 事件添加事件接收器来实现此目的。在重新分配 Source 属性之前,最好随时调用 .Stop() - 查看 此帖子了解更多详细信息

You need to make sure the MediaElement has been opened before you can call .Play() on it - you can do so by adding an event receiver to the MediaOpened event. It would also be good to call .Stop() any time prior to reassigning the Source property - take a look at this thread for more details.

世界和平 2024-10-27 07:30:36

如果没有事件处理程序,这个问题就无法解决。如下所述进行操作。

 <MediaElement x:Name="note1" Source="test.mp3" AutoPlay="False" />

 private void btn1_Click(object sender, RoutedEventArgs e)
 {
   note1.Source = new Uri("test.mp3", UriKind.Relative);
   note1.MediaOpened += new RoutedEventHandler(note1_MediaOpened);
 }

  void note1_MediaOpened(object sender, RoutedEventArgs e)
    {
        note1.Play();
    }

这是完美的作品。享受...

This can't be solved without an Eventhandler. Do as mentioned below.

 <MediaElement x:Name="note1" Source="test.mp3" AutoPlay="False" />

 private void btn1_Click(object sender, RoutedEventArgs e)
 {
   note1.Source = new Uri("test.mp3", UriKind.Relative);
   note1.MediaOpened += new RoutedEventHandler(note1_MediaOpened);
 }

  void note1_MediaOpened(object sender, RoutedEventArgs e)
    {
        note1.Play();
    }

this is perfectly works. enjoy...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文