Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

31 Days of Mango | Day #20: Creating Ringtones

DZone's Guide to

31 Days of Mango | Day #20: Creating Ringtones

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

Day20-RingtonesDay19-AddingTiltEffectsDay18-ExpressionBlendSampleDataDay17-WindowsAzureDay16-IsolatedStorageExplorer

This article is Day #20 in a series called 31 Days of Mango, and was written by guest author Jerrel Blankenship.  (Yes, I realize the remarkable similarities between our names.)  Jerrel can be reached on Twitter at @thejerrel, and you can find his book, Pro Agile .NET Development with Scrum on Amazon.

Today, we are going to talk about ringtones. With the Mango update of Windows Phone, you as a developer have the ability to write applications that can save a user customized ringtone to their device, for later use. This ability is another way to give the user a chance to have a more customized experience on their phone.

Criteria for audio file

Before you can save a ringtone to the phone, there are some stipulations on the audio file that you need to know.

The audio file must meet the following criteria:

  • It can only be in the WMA or MP3 format
  • It can be no larger than 1MB
  • It be unlocked (DRM free)
  • It can be no longer than approximately 40 seconds

SaveRingtoneTask Chooser method

To save a ringtone from your application to the system, you need to use the Chooser API. The Chooser is an API that allows the caller the ability to launch system applications from their own application. Examples of this include the ability to launch the Contacts app to get information about a person, the Bing Maps application to find directions to a location, and launch the Ringtones application. The Ringtones application is an application that allows you to save a ringtone audio file to the ringtones list in the system. The Chooser task you need to call to launch the Ringtones application is called SaveRingtoneTask.

Creating a custom ringtone application

To better understand this and how to use this task, lets build our own ringtone application that takes an audio file from the app and saves it to the system ringtone list.

image_thumb1

To call the SaveRingtoneTask Chooser, you simply need to instantiate a new instance of this chooser and define a delegate that will be ran once the user is complete with the Ringtones application. The sample below displays the application pictured above. To reference this Chooser task, you need to make sure you included the Microsoft.Phone.Task using.

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace CustomRingtonesExample
{
    public partial class MainPage : PhoneApplicationPage
    {
        private readonly SaveRingtoneTask _customRingtone;

        public MainPage()
        {
            InitializeComponent();
            _customRingtone = new SaveRingtoneTask();
            _customRingtone.Completed += customRingtone_Completed;
        }

        private void customRingtone_Completed(object sender, TaskEventArgs e)
        {
            MessageBox.Show(@"You are back from the Ringtones application. Reference e.TaskResult, in your code,
            if you want to see if the save was successful.");
        }

        private void SaveButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            _customRingtone.Source = new Uri("appdata:/Audio/ExampleRingtone.wma");
            _customRingtone.DisplayName = "Example Custom Ringtone";
            _customRingtone.Show();
        }
    }
}

Things to take note of:

  • You need to define a Completed event handler that will run once the user has completed the task. This method is a great place to check to see what the user did in the task and act accordingly.
  • To call the application, you just need to call the Show() method on the chooser.
  • You can access audio files from data that is part of the application (appdata:) as well as from isolated storage (isostore:).

Summary

To wrap up things for today, custom ringtones give the user a chance to have a more personalized experience with their new Window Phone. By calling the SaveRingtonTask, you can provide a gateway for the user to be able to add this level of customization to their phone.

Have fun coding!

To download a working version of the Windows Phone application covered in this article, click the Download Code button below.

download

Tomorrow, Parag Joshi will be covering a new technology available to Windows Phone developers that has been in very high demand: sockets.  See you then!

toolsbutton

.Net developers: use Highcharts, the industry's leading interactive charting library, without writing a single line of JavaScript.

Topics:

Published at DZone with permission of Jeff Blankenburg, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}