Click here to Skip to main content
14,877,073 members
Articles / Mobile Apps / Windows Phone 7
Technical Blog
Posted 28 Jun 2011

Stats

16.9K views
2 bookmarked

Setting Custom Ringtones from Code [Mango:Beta 1]

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 Jun 2011CPOL1 min read
Set custom ring tones in Windows Phone 7.

Written against pre-release information

One of the new features coming with the next update to Windows Phone 7 is the ability to set custom ring tones. From within code, you can make a ring tone available to a user (it's up to the user to accept the ring tone, so user settings won't ever be changed without user permission). I was looking at the new API for doing this, the SaveRingtonTask().

To use the API, you first need to get the ringtone of interest into Isolated Storage. It can be either an MP3 file or a WMA file up to 30 seconds in length. If the file is a part of your application, just set its build type to "Resource".

file settings

Getting the file from being packed in the application to Isolated Storage is a matter of reading from a resource stream and writing to Isolated Storage.

C#
var
s =
    Application.GetResourceStream(new Uri("/MyApplicationName;component/1up.mp3",
                                            UriKind.Relative));
{
    using (var f = IsolatedStorageFile.GetUserStoreForApplication().CreateFile("1up.mp3"))
    {

        var buffer = new byte[2048];
        int bytesRead = 0;

        do
        {
            bytesRead = s.Stream.Read(buffer, 0, 1024);
            f.Write(buffer, 0, bytesRead);
        } while (bytesRead > 0);

        f.Close();
    }
}

Once the file is in Isolated Storage, you must pass the URL to SaveRingtoneTask(). URIs to Isolated Storage are preceded with "isostore:" (there is also an "appdata:" prefix, but we won't be using it here). Give the ringtone a display name and call the Show method to present the user with the option to save it.

C#
SaveRingtoneTask srt = new SaveRingtoneTask();
srt.DisplayName = "1up";
srt.IsoStore= new Uri("isostore:/1up.mp3", UriKind.Absolute);
srt.IsShareable = true;
srt.Show();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Joel Ivory Johnson
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions

 
-- There are no messages in this forum --