The save ringtone task is used to launch the built-in Ringtones app, which enables the user to save an audio file to the system ringtones list and to optionally set it as the active ringtone on the device.
The ringtones list can be viewed by selecting the Ringtones + Sounds item on the phone’s system settings page.
After the audio file is added to the list, the user can set it as the ringtone for individual contacts in the Contacts application.
When the user finishes saving the contact, cancels out of the task, or an error occurs, the task’s Completed
event is raised.
The SaveRingtoneTask
should be defined as a field in your class, like so:
readonly SaveRingtoneTask saveRingtoneTask = new SaveRingtoneTask();
Subscribe to the SaveRingtoneTask.Completed
event within your class constructor, as shown:
saveRingtoneTask.Completed
+= new EventHandler<TaskEventArgs>(HandleCompleted);
The SaveRingtoneTask
allows you to specify a display name for the ringtone and the URI of the audio file. The audio file can be either located in isolated storage or within an assembly. Initialization and launching of the task is shown in the following excerpt:
saveRingtoneTask.DisplayName = displayName;
saveRingtoneTask.IsShareable = shareable;
saveRingtoneTask.Source = new Uri("appdata:/AudioFiles/Audio01.wma");
//saveRingtoneTask.Source = new Uri("isostore:/AudioFiles/Audio01.wma");
saveRingtoneTask.Show();
The appdata segment of the Source Uri
value indicates that the file is located within the app’s XAP file. In this case, the file is located in a project directory called AudioFiles.
Note
When using the appdata prefix, the file must have its Build Action set to Content; otherwise, it will be unresolvable.
Conversely, the isostore prefix indicates that the file is located in isolated storage. Files located in isolated storage must be placed there by your app.
Ringtone audio files must meet the following requirements:
File must be of type MP3 or WMA.
Files must be less than 1MB in size.
Files must be less than 40 seconds in length.
Files must not have digital rights management (DRM) protection.
When the task’s Show
method is called, the built-in Ringtone app is launched, allowing the user to edit the ringtone’s display name and to optionally set the ringtone as the active ringtone.
The sample for the SaveRingtoneTask
consists of the SaveRingtoneTaskView
page and SaveRingtoneTaskViewModel
class. The view allows the user to enter a display name for the ringtone and to launch the SaveRingtoneTask
via an application bar button. The built-in Ringtone app is shown in Figure 14.34.
When the task returns control to your app, the Completed
event handler is called. The TaskEventArgs
object allows you to determine whether the user successfully saved the ringtone, as shown in the following excerpt:
void HandleTaskCompleted(object sender, TaskEventArgs e)
{
if (e.Error != null)
{
MessageService.ShowError("Unable to save ringtone.");
return;
}
if (e.TaskResult == TaskResult.OK)
{
MessageService.ShowMessage("Ringtone saved.");
}
}
The save ringtone task is a great way to extend an audio app. You could have your app procure audio from third-party websites or from your own website.
3.138.35.193