Friday, June 15, 2012

Simple Music player code for Android.

Simple Music player code for Android.


Here i want to write about my first experience in Android Dev.
This application just play embedded in application mp3 file. That’s all.
In first step i create a simple Android UI. Below you can see this:








The source text (main.xml) of this UI is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:text="@string/play_str"
        android:textSize="15pt"
        android:textStyle="bold"
        android:id="@+id/ButtonPlayStop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <SeekBar
        android:id="@+id/SeekBar01"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/ButtonPlayStop"/>
</RelativeLayout>
In my simple mp3 media player i have few string constans, like “PLAY”, “PAUSE”, etc. The best practice is place this constats in special part of project. That part call values. In this part i Eclipse create for me strings.xml file and change it. Bellow you can see strings.xml content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Android mp3 player</string>
    <string name="play_str">PLAY</string>
    <string name="pause_str">PAUSE</string>
</resources>
It’s very simple and useful practice.
Next i make changes in Activity file (Mp3player.java). Bellow i give you some comments about it:
package com.hrupin.mp3player;
import com.hrupin.mp3player.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;
public class Mp3player extends Activity {
    private Button buttonPlayStop;
    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    private final Handler handler = new Handler();
   
    public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
  initViews();
}
    private void initViews() {
        buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
        buttonPlayStop.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {buttonClick();}});
        mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec);
        seekBar = (SeekBar) findViewById(R.id.SeekBar01);
        seekBar.setMax(mediaPlayer.getDuration());
    seekBar.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {
            seekChange(v);
            return false; }
        });
    }
    public void startPlayProgressUpdater() {
        seekBar.setProgress(mediaPlayer.getCurrentPosition());
        if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
                public void run() {
                    startPlayProgressUpdater();
}
            };
            handler.postDelayed(notification,1000);
        }else{
         mediaPlayer.pause();
            buttonPlayStop.setText(getString(R.string.play_str));
            seekBar.setProgress(0);
        }
    }
    // This is event handler thumb moving event
    private void seekChange(View v){
        if(mediaPlayer.isPlaying()){
            SeekBar sb = (SeekBar)v;
            mediaPlayer.seekTo(sb.getProgress());
     }
    }
    // This is event handler for buttonClick event
    private void buttonClick(){
        if (buttonPlayStop.getText() == getString(R.string.play_str)) {
            buttonPlayStop.setText(getString(R.string.pause_str));
            try{
                mediaPlayer.start();
                startPlayProgressUpdater();
            }catch (IllegalStateException e) {
                mediaPlayer.pause();
            }
        }else {
            buttonPlayStop.setText(getString(R.string.play_str));
            mediaPlayer.pause();
        }
    }
}