Oct 7, 2012

Play RTTTL (Ring Tone Text Transfer Language) with MediaPlayer

Android platform support various media formats, include mRTTTL (Ring Tone Text Transfer Language).

Ring Tone Text Transfer Language (RTTTL) was developed by Nokia to be used to transfer ringtones to cellphone by Nokia. (source: Wikipedia - Ring Tone Transfer Language).

We are going to play the RTTTL ringtone of "Haunted House" listed in the Wikipedia article as an example.

Play RTTTL (Ring Tone Text Transfer Language) with MediaPlayer

Create /res/raw/hauntedhouse.rtttl file to define ring tone of "Haunted House".
HauntedHouse: d=4,o=5,b=108: 2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e., e, 1f4, 1a4, 1d#, 2e., d, 2c., b4, 1a4, 1p, 2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e., e, 1f4, 1a4, 1d#, 2e., d, 2c., b4, 1a4


Modify layout to have buttons to start and stop the ring tone.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    <Button 
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start" />
    <Button 
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop" />

</LinearLayout>


The Java code of the activity.
package com.example.androidrtttl;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 Button buttonStart, buttonStop;
 MediaPlayer mediaPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonStart = (Button)findViewById(R.id.start);
        buttonStart.setOnClickListener(buttonStartOnClickListener);
        buttonStop = (Button)findViewById(R.id.stop);
        buttonStop.setOnClickListener(buttonStopOnClickListener);

    }

    OnClickListener buttonStartOnClickListener
    = new OnClickListener(){

  @Override
  public void onClick(View v) {
   if (mediaPlayer != null){
    mediaPlayer.release();
   }
   
   mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.hauntedhouse);
   mediaPlayer.setOnCompletionListener(mediaplayerCompletionListener);
   mediaPlayer.start();
  }
     
    };
    
    OnClickListener buttonStopOnClickListener
    = new OnClickListener(){

  @Override
  public void onClick(View v) {
   if (mediaPlayer != null){
    mediaPlayer.stop();
    mediaPlayer.release();
   }
   
  }};
  
 OnCompletionListener mediaplayerCompletionListener
 = new OnCompletionListener(){

  @Override
  public void onCompletion(MediaPlayer arg0) {

   mediaPlayer.release();
   
   Toast.makeText(getApplicationContext(), 
     "onCompletion", 
     Toast.LENGTH_LONG).show();
  }};

}


No comments:

Post a Comment

Infolinks In Text Ads