Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid How to obtain media player progress bar and also how to show it on each activity top screen
    text
    copied!<p>Am working on certain app XYZ. In that app having a audio player module. I launch a player launch from an activity using some following code:</p> <pre><code>playButton = (ImageButton) findViewById(R.id.play_image_button); playButton.refreshDrawableState(); playButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { if (playCount % 2 == 0) { if (Data.songProfileArrayList.size() &gt; 0) { SongProfileBean songData = Data.songProfileArrayList .get(0); if ("audio".equals(songData.getSongFileType())) { playButton .setBackgroundResource(R.drawable.pause_button); playBackgroundMP3(Data.URL_BASE + songData.getSongName()); } else if ("video".equals(songData.getSongFileType())) { Intent intent = new Intent(mContext, VedioViewActivity.class); intent.putExtra("VideoUrl", Data.URL_BASE + songData.getSongName()); startActivity(intent); } } } else { // stopPlay(); pauseSong(); // playerLayout.setVisibility(View.INVISIBLE); // playButton.setImageDrawable(getResources().getDrawable(R.drawable.play_button)); playButton.setBackgroundResource(R.drawable.play_button); } playCount++; } }); </code></pre> <p>To explain more about the code: Currently I'm using an same Image Button on click on that the audio song starts from Data.songProfileArrayList an Array list and on another click of same button the song is stoped.</p> <p>Now My Requirement or gist of question:</p> <p>Actually I'm looking for following:</p> <p>1.Is there any things available to also get the progress state of audio . I have gone through <a href="http://developer.android.com/reference/android/media/MediaPlayer.html" rel="nofollow">1.developer docs</a> <a href="http://developer.android.com/guide/topics/media/index.html" rel="nofollow">2.android docs on media</a></p> <p>If yes Also suggest the way to have that status bar on each activity of application.</p> <p>2.Should be able to progress the audio ,start,stop from each activity.</p> <p><strong>Please suggest a valid answer only after going through all details mentioned. Only suggest answer If you have a deep knowledge over that and have done earlier so far</strong> </p> <p><em>Valid Help and suggestions are most welcome</em> </p> <p>Regards,</p> <p>Arpit</p> <hr> <p>Hello After R &amp; D over the problem I am able to get default controllers and progress state .But still want this to be on each activity and should be custom Controllers</p> <pre><code>package com.APP_NAME_XYZ.app; import java.io.IOException; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.MotionEvent; import android.widget.MediaController; import android.widget.TextView; public class AudioPlayer extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl { private static final String TAG = "AudioPlayer"; public static final String AUDIO_FILE_NAME = "audioFileName"; private MediaPlayer mediaPlayer; private MediaController mediaController; private String audioFile; private Handler handler = new Handler(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.audio_player); // audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME); audioFile = "Provide URL of song"; mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(this); mediaController = new MediaController(this); try { mediaPlayer.setDataSource(audioFile); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { Log.e(TAG, "Could not open file " + audioFile + " for playback.", e); } } @Override protected void onStop() { super.onStop(); mediaPlayer.stop(); mediaPlayer.release(); } @Override public boolean onTouchEvent(MotionEvent event) { // the MediaController will hide after 3 seconds - tap the screen to // make it appear again mediaController.show(); return false; } // --MediaPlayerControl // methods---------------------------------------------------- public void start() { mediaPlayer.start(); } public void pause() { mediaPlayer.pause(); } public int getDuration() { return mediaPlayer.getDuration(); } public int getCurrentPosition() { return mediaPlayer.getCurrentPosition(); } public void seekTo(int i) { mediaPlayer.seekTo(i); } public boolean isPlaying() { return mediaPlayer.isPlaying(); } public int getBufferPercentage() { return 0; } public boolean canPause() { return true; } public boolean canSeekBackward() { return true; } public boolean canSeekForward() { return true; } // -------------------------------------------------------------------------------- public void onPrepared(MediaPlayer mediaPlayer) { Log.d(TAG, "onPrepared"); mediaController.setMediaPlayer(this); mediaController.setAnchorView(findViewById(R.id.main_audio_view)); handler.post(new Runnable() { public void run() { mediaController.setEnabled(true); mediaController.show(); } }); } } </code></pre> <p>The audio_player layout is as follows:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_audio_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Now playing:" android:textSize="25sp" android:textStyle="bold" /&gt; &lt;TextView android:id="@+id/now_playing_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_gravity="center" android:text="Now playing.." android:textSize="16sp" android:textStyle="italic" /&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload