i made a mp3 player app in viusal studio 2022 in wpf . the sound is not playing or coming....(urgent)

_ ati 111 Reputation points
2022-08-01T13:54:57.587+00:00

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>  
Developer technologies | Windows Presentation Foundation
{count} votes

Accepted answer
  1. Michael Taylor 60,346 Reputation points
    2022-08-01T14:51:26.783+00:00

    You have no error handling in your code at all. Therefore every place you try to call the media player could potentially be failing and you have no way of knowing why. We cannot help with this. Add error handling around every call you make to your player and see what calls, if any, are failing. For example.

       private void folder_Click(object sender, RoutedEventArgs e)  
       {  
          //Use Microsoft.Win32 here, not Winforms...  
          OpenFileDialog ofd = new OpenFileDialog();  
          ofd.Filter = "MP3 Files (*.mp3)|*.mp3|Audio File (*.vma)|*.vma";  
                       
          bool result = ofd.ShowDialog() ?? false;               
          if (!result)  
             return;  
         
          try  
          {  
             mp3Player.open(ofd.FileName);  
          } catch (Exception e)  
          {  
             MessageBox.Show(e.Message, "Open Failed", MessageBoxButton.OK, MessageBoxImage.Error);  
          };  
       }  
    

    Note, don't mix WPF and Winforms. Use strict WPF for everything including OpenFileDialog and MessageBox.
    I can see several issues with trying to get this code to work once you've played an MP4 once but let's get past the first attempt.


1 additional answer

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-08-01T14:32:59.467+00:00

    You forgot spaces in the string :

     string Format = @"open ""{0}"" type MPEGVideo alias MediaFile";  
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.