我已经使用seekbar android实现了MediaPlayer应用程序在正常应用程序中工作正常但是我在寻呼机适配器中的onInstantianti Item方法中集成无法正常工作。
class CustomPagerAdapter extends PagerAdapter implements
OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
Context mContext;
LayoutInflater mLayoutInflater;
int[] mResources = {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher
};
private ImageButton buttonPlayPause, volumeIncrease;
public SeekBar seekBarProgress;
public TextView startTimeSeekBar, endTimeSeekBar;
private AudioManager myAudioManager;
public MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds;
private final Handler handler = new Handler();
String[] songs={
"http://f23.wapka-files.com/download/d/5/7/1955268_d5778ac53d7f6417b43ae324.mp3/18da4fddd79b1d40b618/Ka%2BKha%2BGha.mp3",
"http://f23.wapka-files.com/download/8/0/4/1955268_804038fe90ec24375c079bc5.mp3/ca37bbb63b025a2fa450/Dhadaam%2BDhadaam.mp3",
"http://mykuttywap.in/2013-mp3/dl/load/Malayalam%20Mp3/Malayalam%20(2015)/100%20Days%20of%20Love/Ninnekkaanaan.mp3",
"http://f23.wapka-files.com/download/d/5/7/1955268_d5778ac53d7f6417b43ae324.mp3/18da4fddd79b1d40b618/Ka%2BKha%2BGha.mp3",
"http://f23.wapka-files.com/download/8/0/4/1955268_804038fe90ec24375c079bc5.mp3/ca37bbb63b025a2fa450/Dhadaam%2BDhadaam.mp3",
"http://mykuttywap.in/2013-mp3/dl/load/Malayalam%20Mp3/Malayalam%20(2015)/100%20Days%20of%20Love/Ninnekkaanaan.mp3"};
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
buttonPlayPause = (ImageButton) itemView.findViewById(R.id.ButtonTestPlayPause);
buttonPlayPause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/**
* ImageButton onClick event handler. Method which start/pause
* mediaplayer playing
*/
try {
mediaPlayer.setDataSource(songs[position]); // setup song from
// http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3
// URL to mediaplayer data
// source
mediaPlayer.prepare(); // you must call this method after setup
// the datasource in setDataSource
// method. After calling prepare() the
// instance of MediaPlayer starts load
// data from URL to internal buffer.
} catch (Exception e) {
e.printStackTrace();
}
mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets
// the
// song
// length
// in
// milliseconds
// from
// URL
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
Toast.makeText(mContext, "Displaying here", 5).show();
buttonPlayPause.setImageResource(mResources[position]);
// buttonPlayPause.setImageResource(R.drawable.pause);
} else {
mediaPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.play);
}
primarySeekBarProgressUpdater();
}
});
// Increasing of the volume
volumeIncrease = (ImageButton) itemView.findViewById(R.id.VolumeIncrease);
volumeIncrease.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myAudioManager.adjustVolume(AudioManager.ADJUST_RAISE,
AudioManager.FLAG_SHOW_UI);
}
});
// start time for the seek bar
startTimeSeekBar = (TextView) itemView.findViewById(R.id.StartTimeSeekBar);
// endTimefor the seek bar displaying
endTimeSeekBar = (TextView) itemView.findViewById(R.id.EndTimeSeekBar);
// Fetching of the seekbar and setting the max width is 99 from 0.
seekBarProgress = (SeekBar) itemView.findViewById(R.id.SeekBarTestPlay);
seekBarProgress.setMax(99); // It means 100% .0-99
seekBarProgress.setOnTouchListener(this);
// creating the media player have been did here
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
/**
* Method which updates the SeekBar primary progress by current song playing
* position
*/
private void primarySeekBarProgressUpdater() {
seekBarProgress.setProgress((int) (((float) mediaPlayer
.getCurrentPosition() / mediaFileLengthInMilliseconds) * 100)); // This
// math
// construction
// give
// a
// percentage
// of
// "was playing"/"song length"
int secondsStart = (int) (mediaPlayer.getCurrentPosition() / 1000) % 60;
int minutesStart = (int) ((mediaPlayer.getCurrentPosition() / (1000 * 60)) % 60);
int secondsEnd = (int) (mediaFileLengthInMilliseconds / 1000) % 60;
int minutesEnd = (int) ((mediaFileLengthInMilliseconds / (1000 * 60)) % 60);
startTimeSeekBar.setText("" + minutesStart + "." + secondsStart);
startTimeSeekBar.setTextColor(Color.parseColor("#FFFFFF"));
endTimeSeekBar.setText("" + minutesEnd + "." + secondsEnd);
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.SeekBarTestPlay) {
/**
* Seekbar onTouch event handler. Method which seeks MediaPlayer to
* seekBar primary progress position
*/
if (mediaPlayer.isPlaying()) {
SeekBar sb = (SeekBar) v;
int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100)
* sb.getProgress();
mediaPlayer.seekTo(playPositionInMillisecconds);
}
}
return false;
}
@Override
public void onCompletion(MediaPlayer mp) {
/**
* MediaPlayer onCompletion event handler. Method which calls then song
* playing is complete
*/
buttonPlayPause.setImageResource(R.drawable.play);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
/**
* Method which updates the SeekBar secondary progress by current song
* loading from URL position
*/
seekBarProgress.setSecondaryProgress(percent);
}
}**strong text**