播放独立存储中的媒体文件

发布于 2024-09-28 22:54:57 字数 910 浏览 2 评论 0原文

我正在从 Web 服务读取保存为字节流的 wav 文件,并希望在显示我的记录时播放它。电话 7 应用程序。

我的方法是在导航到记录时将字节流保存到独立存储中的 wav 文件,然后在单击按钮并播放时将媒体播放器 (MediaElement1) 的源设置为该源。

下面是我的“PlayButton”中当前的代码。 (大小与字节流匹配,但没有音频结果)。如果我将流设置为作为资源存储的 WAV 文件,它确实可以工作,所以也许我只需要知道如何将 Uri 设置为独立存储文件。

(例如以下代码有效)

Mediaelement1.Source = new Uri("SampleData\\MyMedia.wav",UriKind.Relative) Works
Mediaelement1.Position = new TimeSpan(0,0,0,0) ;
Mediaelement1.Play() ;

这是我的代码示例...有什么想法吗?

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication() ;
IsolatedStorageFileStream str = new IsolatedStorageFileStream(
    "MyMedia.wav", FileMode.Open, isf) ;
long size = str.Length;
mediaelement mediaelement = new MediaElement() ;
mediaelement.SetSource(str) ;
mediaElement1.Source = mediaelement.Source ;
mediaElement1.Position = new TimeSpan(0, 0, 0, 0);
mediaElement1.Play();

I am reading a wav file saved as a byte stream from a web service and want to play it back when my record is displayed. Phone 7 app.

My approach has been to save the byte stream to a wav file in isolated storage upon navigating to the record and subsequently set the source of my media player (MediaElement1) to that source when a button is clicked and play it back.

Below is my current code in my "PlayButton". (size matches byte stream but no audio results). If I set the stream to a WAV file stored as a resource it does work so perhaps I just need to know how to set the Uri to the Isolated storage file.

(e.g. following code works)

Mediaelement1.Source = new Uri("SampleData\\MyMedia.wav",UriKind.Relative) Works
Mediaelement1.Position = new TimeSpan(0,0,0,0) ;
Mediaelement1.Play() ;

Here is my code sample... any ideas?

IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication() ;
IsolatedStorageFileStream str = new IsolatedStorageFileStream(
    "MyMedia.wav", FileMode.Open, isf) ;
long size = str.Length;
mediaelement mediaelement = new MediaElement() ;
mediaelement.SetSource(str) ;
mediaElement1.Source = mediaelement.Source ;
mediaElement1.Position = new TimeSpan(0, 0, 0, 0);
mediaElement1.Play();

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

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

发布评论

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

评论(2

最后的乘客 2024-10-05 22:54:57

您不必创建 2 个媒体元素。只需直接在 mediaElement1 上调用 .SetSource 即可。

我有类似的代码,将 MediaElement 源设置为独立存储中的电影,并且工作正常:

using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var isfs = new IsolatedStorageFileStream("trailer.wmv", FileMode.Open, isf))
    {
        this.movie.SetSource(isfs);
    }
}

通过上面的代码,movie 是我已经在 XAML 中创建的 MediaElement 并将 autoPlay 设置为 true。

当我第一次让它工作时,我确实遇到了一些问题。

我建议尝试以下操作来帮助调试:

  1. 确保文件已正确且完整地写入隔离存储。

  2. 处理 MediaFailed 事件以找出它不工作的原因。

You shouldn't have to create 2 media elements. Just call .SetSource on mediaElement1 directly.

I have similar code which sets the MediaElement source to a movie in isolated storage and that works fine:

using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var isfs = new IsolatedStorageFileStream("trailer.wmv", FileMode.Open, isf))
    {
        this.movie.SetSource(isfs);
    }
}

With the above, movie is a MediaElement I've already created in XAML and set autoPlay to true.

I did have a few issues with the above when first getting it working.

I suggest trying the following to help debug:

  1. Ensure that the file has been written to isolated storage correctly and in it's entirety.

  2. Handle the MediaFailed event to find out why it isn't working.

后eg是否自 2024-10-05 22:54:57

我注意到的一件事是,当设备连接到计算机时,音频无法工作...当尝试听 mp3 文件时,我花了几个小时在这个设备上。

One thing I noticed is that when the device is tethered to the computer the Audio doesn't work... Spent a couple hours with this one when trying to listen to mp3 files.

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