How to: Use the Save Ringtone Task for Windows Phone
March 23, 2012
Use the save ringtone task to enable users to save an audio file to the system ringtones list. This task launches the Ringtones application, where the user saves the ringtone and can optionally set it as their default ringtone. Once the audio file is added to the list, the user can set it as the ringtone for individual contacts in the Contacts application.
By using Choosers, you help provide a consistent user experience throughout the Windows Phone platform. For more information, see Launchers and Choosers Overview for Windows Phone.
Ringtone audio files must meet the following requirements.
Files must be of type MP3 or WMA.
Files must be less than 1 MB in size.
Files must be less than 40 seconds in length.
Files must not have digital rights management (DRM) protection.
To use the save ringtone task
Add the following statement to your code.
Declare the task object. It must have page scope, so declare it in your page before the constructor.
Add the following code to your page constructor. This code initializes the task object, and identifies the method to run after the user completes the task.
Add the following code to your application wherever you need it, such as in a button click event. To test this procedure, you can put the code in the page constructor. This is the code to launch the task.
To test this code, you need to add an audio file to your application and name it myTone.wma. You can look for sample ringtones in C:\ProgramData\Microsoft\Windows\Ringtones, or search your computer for any short file with a .wma extension.
try { saveRingtoneChooser.Source = new Uri("appdata:/myTone.wma"); //saveRingtoneChooser.Source = new Uri("isostore:/myTone.wma"); saveRingtoneChooser.DisplayName = "My custom ringtone"; saveRingtoneChooser.Show(); } catch (System.InvalidOperationException ex) { MessageBox.Show("An error occurred."); }
Note:Use appdata: for audio files that are part of the application project. They are bundled into the XAP file and stored in the installation directory of the application. Use isostore: for audio files that are stored in isolated storage.
Add the code for the completed event handler to your page. This code runs after the user completes the task. You can check to see whether the ringtone was saved successfully.
void saveRingtoneChooser_Completed(object sender, TaskEventArgs e) { switch (e.TaskResult) { //Logic for when the ringtone was saved successfully case TaskResult.OK: MessageBox.Show("Ringtone saved."); break; //Logic for when the task was cancelled by the user case TaskResult.Cancel: MessageBox.Show("Save cancelled."); break; //Logic for when the ringtone could not be saved case TaskResult.None: MessageBox.Show("Ringtone could not be saved."); break; } }