Create an application that will play a media file from the memory card.

AndroidMediaPlayer.java
package com.play;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidMediaPlayer extends Activity {
MediaPlayer mediaPlayer;
Button buttonPlayPause, buttonQuit;
TextView textState;
private int stateMediaPlayer;
private final int stateMP_Error = 0;
private final int stateMP_NotStarter = 1;
private final int stateMP_Playing = 2;
private final int stateMP_Pausing = 3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonPlayPause = (Button)findViewById(R.id.playpause);
buttonQuit = (Button)findViewById(R.id.quit);
textState = (TextView)findViewById(R.id.state);
buttonPlayPause.setOnClickListener(buttonPlayPauseOnClickListener);
buttonQuit.setOnClickListener(buttonQuitOnClickListener);
initMediaPlayer();
}
private void initMediaPlayer()
{
String PATH_TO_FILE = "/sdcard/music.mp3";
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(PATH_TO_FILE);
mediaPlayer.prepare();
Toast.makeText(this, PATH_TO_FILE,
Toast.LENGTH_LONG).show();
stateMediaPlayer = stateMP_NotStarter;
textState.setText("- IDLE -");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.toString(),
Toast.LENGTH_LONG).show();
stateMediaPlayer = stateMP_Error;
textState.setText("- ERROR!!! -");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.toString(),
Toast.LENGTH_LONG).show();
stateMediaPlayer = stateMP_Error;
textState.setText("- ERROR!!! -");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.toString(),
Toast.LENGTH_LONG).show();
stateMediaPlayer = stateMP_Error;
textState.setText("- ERROR!!! -");
}
}
Button.OnClickListener buttonPlayPauseOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(stateMediaPlayer){
case stateMP_Error:
break;
case stateMP_NotStarter:
mediaPlayer.start();
buttonPlayPause.setText("Pause");
textState.setText("- PLAYING -");
stateMediaPlayer = stateMP_Playing;
break;
case stateMP_Playing:
mediaPlayer.pause();
buttonPlayPause.setText("Play");
textState.setText("- PAUSING -");
stateMediaPlayer = stateMP_Pausing;
break;
case stateMP_Pausing:
mediaPlayer.start();
buttonPlayPause.setText("Pause");
textState.setText("- PLAYING -");
stateMediaPlayer = stateMP_Playing;
break;
}
}
};
Button.OnClickListener buttonQuitOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.release();
finish();
}
};
}


Main.xml
<?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"
>
<Button
android:id="@+id/playpause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
/>
<Button
android:id="@+id/quit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Quit!"
/>
<TextView
android:id="@+id/state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.play"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name="AndroidMediaPlayer">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


Output



To download follow this Link : PlayMusic.zip

No comments:

Post a Comment