Chris Tanev
Chris Tanev

Reputation: 212

MVVM MediaElement.Play() doesn't work/no exception(in ViewModel)

So i have UWP app and in the xaml i have media element passed as a parameter in a command(on click).Everything executes without exceptions but it doesn't play the media. My question is why does it not work ?

Xaml

 <MediaElement Name="media" AutoPlay="True"  Source="{Binding Song.SongToPlay}"></MediaElement>
 <Button x:Name="play"  Content="button" Command="{Binding OnPlayButton}" CommandParameter="{Binding ElementName=media , Mode=OneTime}" />

ViewModel

public ICommand OnPlayButton { get; set; }

public MainViewModel(IViewModelService iService)
    {
        Song = new Song();
        _iVmService = iService;
        OnPlayButton = new CommandBase(Play); 


        public void Play(object o)
    {
        try
        { //just testing
            _uiControls= new UIControls();
            _uiControls.MediaElement = o as MediaElement;
            _uiControls.MediaElement.Source = Song.SongToPlay = new Uri(@"C:\Users\blabla\Desktop\ZHU.mp3", UriKind.Absolute);
            _uiControls.MediaElement.Play(); //doesnt do anything
         }
        catch (Exception exception) //never hit breakpoing here
        {

        }


  public class UIControls : ModelBase
{


    private MediaElement _mediaElement;

    public MediaElement MediaElement
    {
        get { return _mediaElement; }
        set { _mediaElement = value; OnPropertyChanged(); }
    }
}

Upvotes: 0

Views: 350

Answers (1)

T&#243;th Tibor
T&#243;th Tibor

Reputation: 1565

You cannot play files outside from the application's storage C:\Users\blabla\Desktop\ZHU.mp3 (interner urls still allowed). You need to use FileOpenPicker to play music files which are not belonged to the application.

Upvotes: 1

Related Questions