In this article we will discuss how to create custom ringtone in Windows Phone 7. There are few basic requirements to create ringtone in Windows Phone 7.
1. File must be of type MP3 or WMA. 2. File must be less than 1 MB in size. 3. File must be less than 40 seconds in length. 4. File must not have digital rights management (DRM) protection.
Let's write code to create custom ringtone in Windows Phone 6.
Step 1: Create a button in the MainPage.xaml to save the custom ringtone.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button x:Name="btnSaveRingTone" Click="btnSaveRingTone_Click" Content="Save Ringtone" /> </Grid>
Step 2: Add Microsoft.Phone.Tasks using directive.
We will use SaveRingtoneTask of Mircrosoft.Phone.Tasks. It allows users to savea ringtone from application to the system ringtone list.

SaveRingtoneTask creates new instance of SaveRingtoneTask
Show opens the ringtones application
DisplayName identifies the ringtone in the picker.
IsShareable indicates whether ringtone can be accessible to other application or not.
Source defines the local file path of audio files.
TaskEventArgs is EventArgs for Completed Event.
Completed event triggers when chooser task is completed.
Step 3: Create a class level variable of SaveRingtoneTask
SaveRingtoneTask customRingtone;
Step 4: Create a instance of SaveRingtoneTask and attache Completed event handler in the constructor.
// Constructor
public MainPage() { InitializeComponent(); customRingtone = new SaveRingtoneTask(); customRingtone.Completed += new EventHandler<TaskEventArgs>(customRingtone_Completed); }
Step 5: Create a button handler of btnSaveRingtone to save the custom ringtone
private void btnSaveRingTone_Click(object sender, System.Windows.RoutedEventArgs e) { customRingtone.Source = new Uri("appdata:/Ring01.wma"); customRingtone.DisplayName = "Custom Ringtone"; customRingtone.Show(); }
Step 6: Now we will place code Completed event handler which will trigger after the saving task is completed.
void customRingtone_Completed(object sender, TaskEventArgs e) { switch (e.TaskResult) { case TaskResult.OK: MessageBox.Show("Ringtone saved successfully."); break; case TaskResult.Cancel: MessageBox.Show("Save cancelled."); break; case TaskResult.None: MessageBox.Show("Ringtone could not be saved."); break; } }
Step 7: Now run the application.

Click on Save Ringtone below screen will appear.

On click of tick mark on bottom of above screen will save the ringtone.

This ends the article of saving custom ringtone.
|