简体   繁体   中英

Trying to play a .MP3 using the MediaPlayer class; doesn't work

I have little experience at playing.MP3 files in C# applications, but I have a need to do so. I tried writing a simple, .NET Core 3 console app over the weekend. It worked fine. I thought it would be easy to duplicate what I'd done at home to my work environment, but it's not playing to.MP3 file. Here's the code that I have in a WPF app we're writing:

private MediaPlayer mediaPlayer;
private void PlayDrinkingSound()
{
    if (mediaPlayer == null)
    {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.Open(new Uri(Environment.CurrentDirectory + @"\Audio\Slurping-SoundBible.com-755296861.mp3", UriKind.RelativeOrAbsolute));
    }
    mediaPlayer.Play();
}

The differences between the two is at home I wrote a .NET Core 3.1 console app. At work its a .NET 4.5.2 WPF app.

I've set the Build Action on the.MP3 to Resource.

here maybe

When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content and set CopyToOutputDirectory to PreserveNewest or Always.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM