Android allows you to play a sound or a video file in different ways. In this tip, I am going to write an example on playing a sound file using the MediaPlayer class. The MediaPlayer class is able to play both sound and video files.
To play a sound file, first you need to create an instance of the MediaPlayer using the create(Context c,int resid). The below are some commonly used methods of the MediaPlayer class.
-getCurrentPostion() gets the current position in milliseconds of the media.
-getDuration() returns the total time in milliseconds of the media.
-isPlaying() indicates that the soundis playing.
-pause() pauses the current playing task.
-reset() resets the MediaPlayer to its uninitialized state.
-seekTo(int position) seeks to specified time postion.
-start() starts playing the file.
Now to demonstrate the MediaPlayer example, you will create a new Android project. Then modify the acitivity_main.xml file to add one TextView, one ProgressBar, and three Buttons as shown below. The TextView displays the current time of playing and the total time of the media file. You need a sound file in the res/raw folder to play. In my case, I have kalimba.mp3 file. The ProgressBar visually shows the progress of playing the file. The three Buttons are Start, Pause, and Stop buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/txt_current"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30sp"
/>
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/bt_play"
android:text="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playSound"
/>
<Button
android:id="@+id/bt_pause"
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pauseSound"
/>
<Button
android:id="@+id/bt_stop"
android:text="Stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stopSound"
/>
</LinearLayout>
</LinearLayout>
package com.example.andtip;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
MediaPlayer mPlayer;
TextView tv;
Handler handler;
ProgressBar pb;
int maxTime;
int curTime;
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlayer=MediaPlayer.create(this, R.raw.kalimba);
handler=new Handler();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//create a LayoutTransition object
return true;
}
public void playSound(View view){
//start playing sound
mPlayer.start();
//get file duration
maxTime=mPlayer.getDuration();
//get start/current position in milliseconds
curTime=mPlayer.getCurrentPosition()/1000;
//set starting time to textview
tv.setText(curTime+"s/"+maxTime/1000+"s");
//set maximum value to progressbar
pb.setMax(maxTime/1000);
//set the start time to progressbar
pb.setProgress(curTime);
//update the textview and progressbar while the sound is playing
handler.post(UpdateTime);
}
public void pauseSound(View view){
if(mPlayer.isPlaying())
//pause the media staying on the current position
mPlayer.pause();
}
public void stopSound(View view){
if(mPlayer.isPlaying()){
//pause and back to the beginning of media
mPlayer.pause();
mPlayer.seekTo(0);
}
}
Runnable UpdateTime=new Runnable(){
public void run(){
if(mPlayer.isPlaying()){
curTime=mPlayer.getCurrentPosition()/1000;
tv.setText(curTime+"s/"+maxTime/1000+"s");
pb.setProgress(curTime);
//delay 1s before next call
handler.postDelayed(this, 1000);
}
}
};
public void onStart(){
super.onStart();
tv=(TextView)findViewById(R.id.txt_current);
pb=(ProgressBar)findViewById(R.id.progress_bar);
}
}
Posted by: Dara | post date: 07-12-2014 | Subject: Android Apps Development
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.