Thursday, November 25, 2010

A simple exercise to play MIDI audio using MediaPlayer

MediaPlayer class can be used to control playback of audio/video files and streams.

A simple exercise to play MIDI audio using MediaPlayer

Put a MIDI file into the res/raw folder of your project, where the Eclipse plugin (or aapt) will find it and make it into a resource that can be referenced from your R class. "midi_sound.mid" in my exercise.

Modify main.xml to have two buttons to play and pause.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PLAY -"
/>
<Button
android:id="@+id/pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- PAUSE -"
/>
</LinearLayout>


Modify source code, AndroidAudioPlayer.java.
package com.exercise.AndroidAudioPlayer;

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

public class AndroidAudioPlayer extends Activity {

MediaPlayer mediaPlayer;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mediaPlayer = MediaPlayer.create(this, R.raw.midi_sound);

Button buttonPlay = (Button)findViewById(R.id.play);
Button buttonPause = (Button)findViewById(R.id.pause);
buttonPlay.setOnClickListener(buttonPlayOnClickListener);
buttonPause.setOnClickListener(buttonPauseOnClickListener);
}

Button.OnClickListener buttonPlayOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
Toast.makeText(AndroidAudioPlayer.this,
"mediaPlayer.start()",
Toast.LENGTH_LONG).show();
}
}
};

Button.OnClickListener buttonPauseOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
Toast.makeText(AndroidAudioPlayer.this,
"mediaPlayer.pause()",
Toast.LENGTH_LONG).show();
}
}
};

}


Download the files.

Related articles:
- Play audio resources using SoundPool
- Play foreground and background music using SoundPool and MediaPlayer
- Play 3gp video file using MediaPlayer

6 comments:

Ashimita Adusumilli said...

Thanks for the code. That is a good example. :)

homEWork said...

How to show the lyric from midi files???

12358 said...

Hi, thanks and I want know this too ..."How to show the lyric from midi files???" using android.mediaplayer?

The MIDI files include the lyrics via http://www.recordingblogs.com/sa/tabid/88/Default.aspx?topic=MIDI+Lyrics+meta+message

So, is it possible?.

Unknown said...

what the source code how to rec in midi file ?

Anonymous said...

Where do I specify which file is to be played? Also I guess the android media player isn't able to mute certain channels or increase/decrease speed or change instruments, right?

Unknown said...

Thank you very much Brother... :)
you save my time....