i was coding for mp3 player while watching youtube tutorial.
the links are here.
https://youtu.be/q03vuxGpp3Y
the problem is i watched many tutorials and tried none of them worked but this one did.
i wrote all code
but when i played. the sound in not coming .
help me. please
Blockquote
// folder open music
private void folder_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "MP3 Files (*.mp3)|*.mp3|Audio File (*.vma)|*.vma";
if (ofd.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
mp3Player.open(ofd.FileName);
}
else
{
System.Windows.Forms.MessageBox.Show("No Selection", "Empty");
}
}
private void Button_Click_8(object sender, RoutedEventArgs e)
{
mp3Player.play();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
mp3Player.stop();
}
i made a class for this. the code is for that :::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace TMA
{
class Mp3Player
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string lpstringCommand, StringBuilder lpstrReturnString, int uReturnLength,int hwdCallBack);
public void open(string File)
{
string Format = @"open""{0}""type MPEGVideo alias MediaFile";
string command = string.Format(Format,File);
mciSendString(command, null, 0, 0);
}
public void play()
{
string command ="play MediaFile";
mciSendString(command, null, 0, 0);
}
public void stop()
{
string command = "stop MediaFile";
mciSendString(command, null, 0, 0);
}
}
}
and the xaml code is::
<Grid Background="#FFC3BABA">
<Rectangle HorizontalAlignment="Center" Height="47" Margin="0,1,0,0" VerticalAlignment="Top" Width="459" Fill="#FFB51C1C"/>
<Button Content="Button" HorizontalAlignment="Left" Height="25" Margin="405,12,0,0" VerticalAlignment="Top" Width="47"/>
<TextBox HorizontalAlignment="Left" Height="26" Margin="77,223,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="322"/>
<Label Content="Label" HorizontalAlignment="Left" Height="24" Margin="146,185,0,0" VerticalAlignment="Top" Width="182"/>
<Button x:Name="folder" Content="folder" HorizontalAlignment="Left" Height="34" Margin="43,300,0,0" VerticalAlignment="Top" Width="66" Click="folder_Click"/>
<Button x:Name="btnBack1" Content="previous" HorizontalAlignment="Left" Height="33" Margin="124,297,0,0" VerticalAlignment="Top" Width="91" Click="btnBack1_Click"/>
<Button Content="Play" HorizontalAlignment="Left" Height="31" Margin="229,297,0,0" VerticalAlignment="Top" Width="55" Click="Button_Click_8"/>
<Button x:Name="btnNext1" Content="Next" HorizontalAlignment="Left" Height="31" Margin="303,297,0,0" VerticalAlignment="Top" Width="55"/>
<Button x:Name="btnStop" Content="stop" HorizontalAlignment="Left" Height="31" Margin="366,298,0,0" VerticalAlignment="Top" Width="55" Click="btnStop_Click"/>
</Grid>