开发者

Strange behavior with WPF MediaElement

I am current using MediaElement to play a variety of different files and I seem to have most of it working.

One thing I noticed is that Audio files (in this case mp3's specifically) refuse to play on the first attempt. Sometimes you can hear a millisecond (very unattractive) worth of sound. More like a blip and then nothing. Any subsequent attempt to load music works just fine, odd. Videos will play on the first attempt, and so will streamed media. This seems to only apply to local audio files.

The code that starts both audio and video files are pretty much identical.

private void lvVideos_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
    {
        var depObj = e.OriginalSource as DependencyObject;

        if (depObj != null)
        {
            var parent = depObj.FindVisualAncestor<ListViewItem>();
            if (parent != null && lvVideos.SelectedItem != null)
            {
                State = PlayState.Closed;

                Video video = lvVideos.SelectedItem as Video;
                if (video == null) return;

                lblTrackName.Text = video.Title;

                MediaPlayer.Source = null;
                MediaPlayer.Source = new Uri(video.Location);

                CurrentMedia = MediaType.Video;
     开发者_如何学运维           State = PlayState.Playing;
            }
        }
    }

    private void lvMusic_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
    {
        var depObj = e.OriginalSource as DependencyObject;

        if (depObj != null)
        {
            var parent = depObj.FindVisualAncestor<ListViewItem>();
            if (parent != null && lvMusic.SelectedItem != null)
            {
                State = PlayState.Closed;

                Music song = lvMusic.SelectedItem as Music;
                if (song == null) return;

                lblTrackName.Text = song.Title;

                MediaPlayer.Source = null;
                MediaPlayer.Source = new Uri(song.Location);

                CurrentMedia = MediaType.Music;
                State = PlayState.Playing;
            }
        }
    }

As you can see I attempted to null the source property prior to loading the audio to no avail. I have managed to come up with a dirty hack of a workaround. Which involved setting the source to a file that is gaurenteed to fail (the app's .exe) and playing it as the app initialized. This allows for the first music file loaded to play properly.

Has anybody else come across this before? and are there any fixes?

EDIT: omg I feel stupid. apparently the culprit was mediaElement.ScrubbingEnabled = true; which (by the documentation) is a seemingly useful option, perhaps it should only be enabled for remote streams?


Apparently the culprit was mediaElement.ScrubbingEnabled = true; which (by the documentation) is a seemingly useful option, perhaps it should only be enabled for remote streams?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜