Windows 8 Windows 8 Metro App MediaElement.SetSource(播放期间无法更改音量)
我正在制作Windows 8 Metro风格的应用程序。 我希望能够同时运行不同的声音并管理它们。为此,我创建了MediaPlayService,它应该包含允许我这样做的方法 我发现一个问题,在“_mediaElement.SetSource()”之后我无法更改卷。我正在呼叫SetVolume,但什么也没发生Windows 8 Windows 8 Metro App MediaElement.SetSource(播放期间无法更改音量),windows-8,microsoft-metro,volume,mediaelement,Windows 8,Microsoft Metro,Volume,Mediaelement,我正在制作Windows 8 Metro风格的应用程序。 我希望能够同时运行不同的声音并管理它们。为此,我创建了MediaPlayService,它应该包含允许我这样做的方法 我发现一个问题,在“_mediaElement.SetSource()”之后我无法更改卷。我正在呼叫SetVolume,但什么也没发生 Initialize(sound); SetVolume(100); Play(); --- this sequence works Initialize(sound
Initialize(sound);
SetVolume(100);
Play(); --- this sequence works
Initialize(sound);
Play();
SetVolume(100); --- does not work (I can not change the volume during playback)
public void SetVolume(int volume)
{
//_m ediaElement.Volume = Math.Round((double)((double)volume / 100), 2);
double dvolume = Math.Round((double)((double)volume / 100), 2);
_mediaElement.SetValue(MediaElement.VolumeProperty, dvolume);
}
string _mediaPath;
public void Initialize(Sound sound)
{
_mediaElement = new MediaElement();
_mediaPath = sound.FilePath;
_mediaElement.AudioCategory = Windows.UI.Xaml.Media.AudioCategory.Communications;
_mediaElement.IsLooping = true;
_mediaElement.MediaFailed += _mediaElement_MediaFailed;
_mediaElement.RealTimePlayback = true;
}
public async void Play()
{
var pack = Windows.ApplicationModel.Package.Current;
var installedLoction = pack.InstalledLocation;
var storageFile = await installedLoction.GetFileAsync(_mediaPath);
if (storageFile != null)
{
var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
_mediaElement.SetSource(stream, storageFile.ContentType);
_mediaElement.Play();
}
}
您的MediaElement不是页面的VisualTree的一部分。因此,您必须处理这些奇怪的行为,如设置音量或位置将无法正常工作 作为解决方案,您可以在XAML文件中创建MediaElement,或者将其从代码隐藏添加到VisualTree(类似于contentGrid.Children.add(_MediaElement)。在后一种情况下,您可能必须在导航到另一个页面之前将其删除,否则下次导航时它可能无法播放