I want to play mp3 audio in my app but nothing is happening
my code is this
XAML:
<Page
x:Class="SunnahForKids.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SunnahForKids"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Black">
<StackPanel Width="900" Height="700" HorizontalAlignment="Center">
<MediaElement HorizontalAlignment="Left" Name="a" AutoPlay="False" Source="azan.mp3" Height="100" Margin="451,299,0,0" VerticalAlignment="Top" Width="100" Volume="100"/>
<Button Content="Button" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,676,0" Height="189" Click="Button_Click"/>
</StackPanel>
</Grid>
</Page>
And here is my .cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media.SpeechSynthesis;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace SunnahForKids
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
a.Play();
}
}
}
and I am struggling with it for 2 days need help . I also followed this link MediaElement in WinRT / Win8 does not work at all
to update my driver but it is up to date.Display driver in Intel R(Q35) express chipset family ( Microsoft Corporation WDDM-1.0) please get me out of here..
I tried your code and the code works, so something strange is going on. I would try to listen for the events mentioned in the link (example further down for devs looking for code example).
Check your event log on the computer (Windows => Applications), if you've missed updates for windows, and check that that mp3 will otherwise play.
Listen for the MediaFailed event (recommend that you do in the docs) and see if you can grab some information there.Code from msdn:
private void videoMediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
// Check the event arguments => e
// get HRESULT from event args
string hr = GetHresultFromErrorMessage(e);
// Handle media failed event appropriately
}
private string GetHresultFromErrorMessage(ExceptionRoutedEventArgs e)
{
String hr = String.Empty;
String token = "HRESULT - ";
const int hrLength = 10; // eg "0xFFFFFFFF"
int tokenPos = e.ErrorMessage.IndexOf(token, StringComparison.Ordinal);
if (tokenPos != -1)
{
hr = e.ErrorMessage.Substring(tokenPos + token.Length, hrLength);
}
return hr;
}
Related
In a WPF application, I have a webbrowser called WebBrowser1. This refers to an HTML page which contains a TextArea to which users can input text.
<html>
<body>
<textarea class="myStudentInput" id="myStudentInput1">
Text to be copied
</textarea>
</body>
</html>
I wish to get this text and potentially also set this text.
I have tried something similar to the javascript way of writing it:
document.getElementById("myStudentOutput1").innerHTML;
such as
HtmlElement textArea = webBrowser1.Document.All["myStudentInput1"];
dynamic textArea = WebBrowser1.Document.GetElementsByID("myStudentInput1").InnerText;
but it doesn't work.
The following solution in Visual Studio 2015 WPF Application works for me.
First, add a reference to the Microsoft HTML COM Library. This is on the COM tab, when you do an "Add Reference" in your project.
Then add the code:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid>
<WebBrowser x:Name="WebBrowser1" HorizontalAlignment="Left" Height="480" Margin="10,10,0,0" VerticalAlignment="Top" Width="770" Source="E:\Others\Dropbox\Programming\Questions.html"/>
<Button x:Name="mySetQuestionButton" Content="Set Question" HorizontalAlignment="Left" Margin="200,520,0,0" VerticalAlignment="Top" Width="75" Click="mySetQuestion"/>
<Button x:Name="myGetAnswerButton" Content="Get Answer" HorizontalAlignment="Left" Margin="350,520,0,0" VerticalAlignment="Top" Width="75" Click="myGetAnswer"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="600,520,0,0" TextWrapping="Wrap" Text="Hello2" VerticalAlignment="Top"/>
</Grid>
</Window>
and
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void mySetQuestion(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myQuestion1");
textArea.innerHTML = "What is 1+1?";
}
private void myGetAnswer(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myStudentInput1");
textBlock.Text = textArea.innerHTML;
}
}
}
but it doesn't work.
I have no idea what that could possibly mean. All you can get is a code snippet that does work:
public partial class Form1 : Form {
private WebBrowser webBrowser1;
private Button button1;
public Form1() {
button1 = new Button { Text = "Test" };
button1.Click += button1_Click;
this.Controls.Add(button1);
webBrowser1 = new WebBrowser { Dock = DockStyle.Fill };
webBrowser1.DocumentText = #"<html><body><textarea class=""myStudentInput"" id=""myStudentInput1"">Text to be copied</textarea></body></html>";
this.Controls.Add(webBrowser1);
}
private void button1_Click(object sender, EventArgs e) {
var elem = webBrowser1.Document.GetElementById("myStudentInput1");
MessageBox.Show(elem.InnerText);
}
}
Which produces:
I just got started in Windows Store App development and I just wanted a very simple application: Pretty much a progres bar that fills up from left to right, but even this task is apparently not achievalbe for me.
I have the following code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace TimeLoader
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private DispatcherTimer refreshTimer;
public MainPage()
{
this.InitializeComponent();
}
void refreshTimer_Tick(object sender, object e)
{
TimePassedBar.Value += 5;
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
TimePassedBar.Value = 50;
new DispatcherTimer();
this.refreshTimer = new DispatcherTimer();
this.refreshTimer.Interval = new TimeSpan(0, 0, 0, 100);
this.refreshTimer.Tick += refreshTimer_Tick;
this.refreshTimer.Start();
}
}
}
<Page
x:Class="TimeLoader.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TimeLoader"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ProgressBar Grid.Row="0" Grid.Column="0" Height="150" Value="75" VerticalAlignment="Center" Name="TimePassedBar"/>
</Grid>
</Page>
Now the same Setup works pretty fine when I do it in WPF but the Tick Event never fires when I start this code built as a Windows store app. Is there something Special I have to pay Attention to when building Windows Store Apps? I have looked high and low and sadly have found nothing on this matter.
Your code was working fine. You just didn't wait long enough to notice. :)
private void Page_Loaded(object sender, RoutedEventArgs e)
{
TimePassedBar.Value = 50;
this.refreshTimer = new DispatcherTimer();
this.refreshTimer.Interval = TimeSpan.FromMilliseconds(100);
this.refreshTimer.Tick += refreshTimer_Tick;
this.refreshTimer.Start();
}
You are setting the TimeSpan as 100 seconds. You need to use the five-parameter overload to get milliseconds. But IMHO it's easier and more readable to just use the FromMilliseconds() method (as above).
Also, you don't need to create a DispatcherTimer object twice, especially when you're going to ignore the first one completely. :)
I seem to have a sluggish UI when trying to scroll any large TextBlocks...
What's the best way to go about this in WP 8.1?
Here's an example I made to illustrate the issue, notice when the app is ran the framerate drops horribly.
I've already looked up BackgroundWorker and it doesn't seem to exist in WP 8.1.
MainPage.xaml
<Page
x:Class="HTTPRequest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HTTPRequest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<ScrollViewer x:Name="scroll" Margin="10,13,0,10" Width="380" MaxZoomFactor="4" VerticalScrollMode="Enabled" HorizontalScrollMode="Disabled" IsTapEnabled="False">
<TextBlock x:Name="RSSData" TextWrapping="Wrap" Text="Loading..." FontSize="22" VerticalAlignment="Top"/>
</ScrollViewer>
<ProgressBar x:Name="prog" Height="16" Margin="101,310,101,0" VerticalAlignment="Top" IsIndeterminate="True"/>
</Grid>
</Page>
MainPage.xaml.cs
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641
namespace HTTPRequest
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
Main();
}
private async void Main()
{
string results = await DownloadXMLDocument();
prog.Visibility = Visibility.Collapsed;
RSSData.Text = results;
}
public async Task<string> DownloadXMLDocument()
{
string URLString = "http://www.packtpub.com/rss.xml";
Uri uri = new Uri(URLString);
XmlDocument xmlDocument = await XmlDocument.LoadFromUriAsync(uri);
return xmlDocument.GetXml();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}
}
}
There is a known issue with large quantities of text in a single TextBox. Instead try using the RichTextBox and see if your performance improves. Sorry you had a problem.
Best of luck!
I have a problem, I want to use a Progress ring in webview but it does not do what I want it to do. The progress ring stays even if the page is loaded. I also want the buttons appear after loading.
Here's my code:
xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// Die Elementvorlage "Leere Seite" ist unter http://go.microsoft.com/fwlink /?LinkId=234238 dokumentiert.
namespace Euregio_Systems
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet werden kann oder auf die innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.webBrowser.Navigate(new Uri("http://www.euregio-systems.com", UriKind.Absolute));
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.webBrowser.InvokeScript("eval", new[] { "history.go(-1)" });
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
this.webBrowser.Navigate(new Uri("http://www.euregio-systems.com", UriKind.Absolute));
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
this.webBrowser.InvokeScript("eval", new[] { "histroy.go(+1)" });
}
private void webBrowser_NavigationCompleted(object sender, NavigatingCancelEventArgs e)
{
pr1.IsActive = false;
pr1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
webBrowser.Visibility = Windows.UI.Xaml.Visibility.Visible;
fwd.Visibility = Windows.UI.Xaml.Visibility.Visible;
backButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
homeButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
}
}
and xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView Name="webBrowser" Visibility="Collapsed"/>
<AppBarButton Visibility="Collapsed" Name="backButton" HorizontalAlignment="Left" Margin="-20,-4,0,0" VerticalAlignment="Top" Click="Button_Click_1" Icon="Back"/>
<AppBarButton Visibility="Collapsed" Name="homeButton" HorizontalAlignment="Left" Margin="56,-4,0,0" VerticalAlignment="Top" Click="Button_Click_2" Icon="Home"/>
<AppBarButton Visibility="Collapsed" Name="fwd" HorizontalAlignment="Left" Margin="132,-4,0,0" VerticalAlignment="Top" Icon="Forward" Click="Button_Click_3"/>
<ProgressRing Visibility="Visible" Name="pr1" HorizontalAlignment="Center" VerticalAlignment="Center" IsActive="True" Height="163" Width="170" Margin="0" RequestedTheme="Light"/>
</Grid>
</Page>
You have to add this line before calling Navigate on the web browser in your code:
this.webBrowser.NavigationCompleted+= webBrowser_NavigationCompleted;
Also you should ideally do this on loaded event of WebView which can be added like this:
<WebView Name="webBrowser" Visibility="Collapsed" loaded="WebViewControl_Loaded"/>
If you have forgotten to subscribe to the event, you should open the Form/Page designer and check for the "Lightning" Icon to display the events you can subscribe to, like in this image
Just look for the NavigationCompleted event and put your callback method name (webBrowser_NavigationCompleted) in.
Though using the events is a bit discouraged these days. MVVM is the preferable method of developing Windows Store Apps (or any modern WPF Application) to fully utilize WPF's power of binding.
Update:
Or if you prefer it, you can also subscribe in code (just make sure you do it after the Components have been initialized):
this.webBrowser.NavigationCompleted += webBrowser_NavigationCompleted;
At first I want to say that i searched on internet and I didn't found anything. I would like to make a dynamic song list, that when user adds a song, new object will be added with name and lenght and when he selects this song he would get a path to the selected song. Here is code to understand better:
XAML:
<Window x:Class="ListBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="SongList" HorizontalAlignment="Left" Height="278" Margin="10,10,0,0" VerticalAlignment="Top" Width="248"/>
<TextBlock HorizontalAlignment="Left" Margin="287,124,0,0" TextWrapping="Wrap" Text="Path" VerticalAlignment="Top" Width="194" Height="22"/>
<Label Content="Song Path" HorizontalAlignment="Left" Margin="287,98,0,0" VerticalAlignment="Top" Width="194"/>
<Button Content="Add Song" HorizontalAlignment="Left" Margin="10,293,0,0" VerticalAlignment="Top" Width="132" Click="Button_Click"/>
</Grid>
and code:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ListBox
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.DefaultExt = ".mp3";
openfile.Filter = "mp3 | *.mp3";
Nullable<bool> result = openfile.ShowDialog();
if (result == true)
{
String file = openfile.FileName;
FileInfo fileinfo = new FileInfo(file);
SongList.Items.Add(fileinfo.Name);
}
}
}
}
so where the "Path" text box is I would like to get current selected song path. Is it possible to make with ItemBox or I need to make array that will save all paths?
I think you should use for your songs, because you're writing a WPF application, an ObservableCollection of your song objects, in order to have a proper ListBox with your updated objects.
Then, you have to create a proper Song class with its own properties, like the SongName or the SongPath. In this class, you will implement the INotifyPropertyChanged interface
and, for each property, you will raise an appropriate OnPropertyChanged event.
With this implementation, once you load a song and add it to the songs list, your ListBox will show the updated collection accordingly.
Further info about the ObservableCollection here.
If you want to have a look at a code sample, in this answer I written an example of developing an ObservableCollection.
at first you might want to have a look at MVVM programming. The concept is not that easy to understand, even harder to apply but once you got it you will be able to create good UI in no-time and it's especially perfect for what you are trying to do.
Now to your problem. FileInfo.Name only stores the file name not the path, so if you don't store the path separately, you won't be able to get it back from just the file name.
KillaKem's solution seems pretty good, however I wouldn't recommend using the Tag property for this. Just create a Collections::Generic::List (please, don't use arrays) class member to store the paths and then use the selectedIndexChange Event to update the PathSearchBox.
using MVVM you could just link the ListBox to a Dictionary which stores both, path and filename and just display the Filename, feel free to research MVVM a bit yourself.
Regards,
Xaser
You could try something along the lines of this,
XAML:
<Window x:Class="ListBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox x:Name="SongList" HorizontalAlignment="Left" Height="278" Margin="10,10,0,0" VerticalAlignment="Top" Width="248"/>
<TextBlock HorizontalAlignment="Left" Margin="287,124,0,0" TextWrapping="Wrap" Text="Path" VerticalAlignment="Top" Width="194" Height="22"/>
<Label Name ="pathLabel" Content="Song Path" HorizontalAlignment="Left" Margin="287,98,0,0" VerticalAlignment="Top" Width="194"/>
<Button Content="Add Song" HorizontalAlignment="Left" Margin="10,293,0,0" VerticalAlignment="Top" Width="132" Click="Button_Click"/>
</Grid>
Back Code:
namespace ListBox
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.DefaultExt = ".mp3";
openfile.Filter = "mp3 | *.mp3";
Nullable<bool> result = openfile.ShowDialog();
if (result == true)
{
String file = openfile.FileName;
FileInfo fileinfo = new FileInfo(file);
SongList.Items.Add(fileinfo.Name);
var pathList = SongList.Tag as List<string>;
pathList.Add(fileinfo.DirectoryName);
SongList.Tag = pathList;
}
}
private void Selection_Changed(object sender, EventArgs e)
{
var myListBox = sender as ListBox;
var myPathList = myListBox.Tag as List<string>;
var filePath = myPathList[myListBox.SelectedIndex];
pathLabel.Content = filePath;
}
}
}
Haven't tested the code but it should be working or close to working.
These is a SelectionChanged event in the listbox you can use. But i would recommend using bi