I have tried Runnable as well but of no use. Here is my code, What can be done here? I have used Parecelable method to get my currently selected song from main activity to this playscreen activity.Now I want to attach seekbar to that 'currsong' I received when my mediaplayer mPlayer starts
package com.musicplayer;
import android.content.ContentUris;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.PowerManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.Random;
public class PlayScreen extends ActionBarActivity {
private Song currsong;
public MainActivity mainActivity;
private MediaPlayer mPlayer;
public TextView songTitle , songArtist;
public ImageView playPause, next, prev;
public SeekBar seekbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_screen);
currsong = (Song)getIntent().getParcelableExtra("currSong");
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.playScreen);
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
songTitle=(TextView)findViewById(R.id.stitle);
songArtist=(TextView)findViewById(R.id.artist);
playPause= (ImageView)findViewById(R.id.playPause);
next= (ImageView)findViewById(R.id.next);
prev= (ImageView)findViewById(R.id.prev);
seekbar=(SeekBar)findViewById(R.id.progress);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mPlayer.isPlaying()){
mPlayer.stop();
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
mainActivity.songPos++;
currsong=mainActivity.songList.get(mainActivity.songPos);
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
seekbar.setMax(mPlayer.getDuration());
playSong();
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mPlayer.isPlaying()){
mPlayer.stop();
try {
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
mainActivity.songPos--;
currsong=mainActivity.songList.get(mainActivity.songPos);
Drawable drawable=new BitmapDrawable(getResources(), currsong.getAlbumart(getApplicationContext()));
relativeLayout.setBackground(drawable);
seekbar.setMax(mPlayer.getDuration());
playSong();
}
});
playPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mPlayer.isPlaying() ){
mPlayer.pause();
playPause.setImageResource(R.drawable.play);
}
else {
mPlayer.start();
playPause.setImageResource(R.drawable.pause);
}
}
});
playSong();
}
public void playSong(){
mPlayer= new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
songTitle.setText(currsong.getTitle());
songArtist.setText(currsong.getArtist());
//set uri
Uri trackUri = ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,currsong.getID());
//set the data source
try{
mPlayer.setDataSource(getApplicationContext(), trackUri);
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
//seekbar.setMax(mPlayer.getDuration());
mPlayer.start();
playPause.setImageResource(R.drawable.pause);
}
public void initMusicPlayer(){
//set player properties
mPlayer.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setOnPreparedListener((MediaPlayer.OnPreparedListener) this);
mPlayer.setOnCompletionListener((MediaPlayer.OnCompletionListener) this);
mPlayer.setOnErrorListener((MediaPlayer.OnErrorListener) this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_play_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
After Searching a lot i have figured out a way to do this and since no one ha answered this question so to help others with same problem I am providing my solved java file code below-