Add a sound in the xNa 3.0 project-play MP3 or WMA audio files

Source: Internet
Author: User
Problem

You want to play an MP3 or WMA audio file in xNa.

Solution

XNa can use the default content pipeline to load MP3 or WMA audio files to a song object. After loading, you can use the static mediaplayer class to play the song object.

Working Principle

First, add the MP3 or WMA file to the xNa project. This is the same as adding an image. See tutorial 3-1. The simplest way is to drag an MP3 or WMA file to the content folder.

You need to link a song object to an MP3 or WMA file.CodeAdd this object to the top:

Song mysong;

Use the content pipeline to load objects in the loadcontent method:

Mysong = content. Load <song> ("sample ");

You can use the following simple command to play a file:

Mediaplayer. Play (mysong );

Mediaplayer is a static class that can only play one sound at a time, which is ideal for playing background music. However, you need to play another sound in the game in a way similar to 7-1 in the tutorial.

The song object and the mediaplayer class both have some useful attributes, such as the length of the song and the playing progress of the current song:

Timespan Togo = mysong. Duration-mediaplayer. playpositon; string mytext = "time remaining for current song:" + Togo. tostring ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.