当我按回音乐停止

我创建了一个流音乐播放器。但我有一些小问题与我的应用程序 当我播放音乐,然后按回音乐停止,请告诉我解决这个问题,我还有1个问题。请告诉我,当歌曲完成时,我做了什么不自动播放下一首歌曲。当歌曲完成后,我试图播放下一首歌曲,然后应用程序停止播放。这里是我的代码当我按回音乐停止

public void get() 

{

seekBarProgress.setMax(99); // It means 100% .0-99

seekBarProgress.setOnTouchListener(this);

mMediaPlayer.setOnBufferingUpdateListener(this);

mMediaPlayer.setOnCompletionListener(this);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

//getting the upload

Upload upload = uploadList.get(i);

if (mMediaPlayer.isPlaying()) {

mMediaPlayer.stop();

mMediaPlayer.reset();

seekBarProgress.setProgress(0);

}

try {

mMediaPlayer.setDataSource(upload.getUrl());

mMediaPlayer.prepareAsync();

} catch (IOException e) {

e.printStackTrace();

}

}

});

}

private void primarySeekBarProgressUpdater() {

seekBarProgress.setProgress((int)(((float)mMediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100));

if (mMediaPlayer.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.length){

if(mMediaPlayer.isPlaying()){

SeekBar sb = (SeekBar)v;

int playPositionInMillisecconds = (mediaFileLengthInMilliseconds

/100) * sb.getProgress();

mMediaPlayer.seekTo(playPositionInMillisecconds);

}

}

return false;

}

@Override

public void onBufferingUpdate(MediaPlayer mp, int percent) {

seekBarProgress.setSecondaryProgress(percent);

}

@Override

public void onCompletion(MediaPlayer mp) {

// Help me also here

}

private void togglePlayPause() {

if (mMediaPlayer.isPlaying()) {

mMediaPlayer.pause();

mPlayerControl.setImageResource(R.drawable.ic_play);

} else {

mMediaPlayer.start();

mPlayerControl.setImageResource(R.drawable.ic_pause);

mediaFileLengthInMilliseconds = mMediaPlayer.getDuration();

primarySeekBarProgressUpdater();

mSelectedTrackImage.setImageResource(R.drawable.images);

}

}

如果有人知道那么请帮助我。

回答:

您的播放器代码在Activity中执行,当按下Back按钮时,活动进入后台,播放停止。

正确的解决方案是创建一个Android服务,简要:

public class PlayerService extends Service implements AudioManager.OnAudioFocusChangeListener { 

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

String action = intent.getAction();

if (PLAY.equals(action))

play()

...

}

注册它androidManifest:

<service 

android:name=".PlayerService"

android:exported="false" >

</service>

并启动它,例如在应用程序类的onCreate

@Override 

public void onCreate() {

super.onCreate();

startService(new Intent(this, PlayerService.class));

bindService(new Intent(this, PlayerService.class), mConnection, Context.BIND_AUTO_CREATE);

}

请参阅这篇文章创建服务:https://developer.android.com/training/run-background-service/create-service.html

回答你的第二个问题,你正在考虑覆盖onCompletion方法。 像往常一样,它应该是这样的(伪代码):

@Override 

public void onCompletion(MediaPlayer mp) {

if (isRepeat) {

play(current);

} else if (isShuffle) {

play(random);

} else {

play(next);

}

}

public void play(index or file or stream){

mp.reset();

mp.setDataSource(get by index or file or stream);

mp.prepare();

mp.start();

}

以上是 当我按回音乐停止 的全部内容, 来源链接: utcz.com/qa/261425.html

回到顶部