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:
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() > 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++;
}
});
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.
Now My Requirement or gist of question:
Actually I'm looking for following:
1.Is there any things available to also get the progress state of audio . I have gone through 1.developer docs 2.android docs on media
If yes Also suggest the way to have that status bar on each activity of application.
2.Should be able to progress the audio ,start,stop from each activity.
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
Valid Help and suggestions are most welcome
Regards,
Arpit
Hello After R & 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
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开发者_如何学Python 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();
}
});
}
}
The audio_player layout is as follows:
<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">
<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" />
<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" />
</LinearLayout>
You will need to change your approach. Use fragments and have one fragment show the progressbar and remain visible throughout. You can have just one activity and change the UI using fragments depending on different states of the application. When the application goes into background use Notification bar to show the media progress bar and allow user to relaunch the application from there.
Please refer how google music app works and follow the recommended approach.
精彩评论