Your experience on this site will be improved by allowing cookies
The MediaPlayer class in Android is used to play and control audio files.
For controlling the audio or video files, the android.media.MediaPlayer class is used.
The MediaPlayer class contains many methods. Some of the important methods of the MediaPlayer class are:
Method | Uses |
public void setDataSource(String path) | It is used to set the data source, i.e., file path or http url, to use. |
public void prepare() | It is used to prepare the player for playback synchronously. |
public void start() | It is used to start or resume the playback. |
public void stop() | It is used to stop the playback. |
public void pause() | It is used to pause the playback. |
public boolean isPlaying() | It is used to check if the media player is playing. |
public void seekTo(int millis) | It is used to seek a specific time in milliseconds. |
public void setLooping(boolean looping) | It is used to set the player for looping or non-looping. |
public boolean isLooping() | It is used to check if the player is looping or non-looping. |
public void selectTrack(int index) | It is used to select a track for the specified index. |
public int getCurrentPosition() | It is used to return the current playback position. |
public int getDuration() | It is used to return the duration of the file. |
public void setVolume(float leftVolume,float rightVolume) | It is used to set the volume on this player. |
In the below example, we will demonstrate how to start, stop, and pause the audio play.
activity_main.xml:
In the activity_main.xml file, we will drag three buttons from the palette to start, stop, and to pause the audio play.
Activity class:(File: MainActivity.java)
In the MainActivity.java file, we will write the code to start, pause, and to stop the audio player.
package com.example.radioapp; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button start,pause,stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start=(Button)findViewById(R.id.button1); pause=(Button)findViewById(R.id.button2); stop=(Button)findViewById(R.id.button3); //creating media player final MediaPlayer mp=new MediaPlayer(); try{ //you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3 mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Music/musicplay.mp3"); mp.prepare(); }catch(Exception e){e.printStackTrace();} start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mp.start(); } }); pause.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mp.pause(); } }); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mp.stop(); } }); } } |
0 comments