Practical development tips for Windows Phone (35): Save music as a ringtone

Source: Internet
Author: User

In Windows Phone 7.5, we can save a piece of music as a ringtone in the program. However, there are some restrictions on the storage of music files:

1. The file must be in MP3 or WMA format.

2. The length of the ringtone file must be less than 40 seconds.

3. No copyright restrictions on ringtone files

4. The ringtone file size must be smaller than 1 MB.

In actual processing, the file we want to save as a ringtone is likely to be a complete song with a size greater than 1 MB. So how can we save the song climax as a ringtone? For example, "on the beach" definitely wants to save what is "on the waves, on the waves.

According to some of my non-professional understandings: A Song generally has two reincarts, And the climax usually appears at the end of each cycle, that is, between 1/4 and 1/3 of a song. Then we can save that part as another file.

The Demo below demonstrates how to save a part of a local song as a ringtone.

First, let's take a look at the intercepted code:

private void SaveIntoIso ()
{
    Stream stream = App.GetResourceStream (new Uri ("mangoRingtones; component / beat_it.mp3", UriKind.Relative)). Stream;

    // read some of them
    stream.Seek (stream.Length / 4, 0);
    byte [] buffer = new byte [stream.Length / 3-stream.Length / 4];
    stream.Read (buffer, 0, buffer.Length);

    // save into isostore
    using (var isostore = IsolatedStorageFile.GetUserStoreForApplication ())
    {
        using (var inp = isostore.OpenFile (fileName, FileMode.OpenOrCreate, FileAccess.Write))
        {
            inp.Write (buffer, 0, buffer.Length);
        }
    }

    stream.Close ();
    stream.Dispose ();
}
Read the stream from the local and save part of it as another file.

Then let's take a look at how we can save audio files as phone ringtones in Windows Phone.

SaveRingtoneTask task = new SaveRingtoneTask ();
task.DisplayName = "beat it";
task.Source = new Uri (@ "isostore: /" + fileName);
task.Completed + = new EventHandler <TaskEventArgs> (task_Completed);
task.Show ();
Note that referencing files in independent storage space can use the new Uri (@ "isostore: /" + fileName);

 

You can find the source code of this article here.

Related Article

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.