您好我正在创建一个类似声音云的应用程序。主要活动中有一个歌曲列表,当用户点击其中一首歌曲时,如果用户点击播放按钮,则会激活新活动如果用户返回主要活动歌曲继续播放,则歌曲也开始播放,但如果用户选择了另一首歌曲并播放,则前一首歌曲继续播放新歌曲。
当用户点击新歌的播放按钮时,如何阻止上一首歌在后台播放?
以下是我的音频播放器活动的代码
public class Songs_Single extends AppMenu {
ListView lv;
static final String[] numbers = new String[] { "one", "two", "three",
"four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen", "twenty", "twenty one",
"twenty two" };
ImageView cover;
ImageView playBtn;
ImageView prevBtn;
ImageView nextBtn;
SeekBar songSeekBar;
SeekBar volumeSeekBar;
ImageView actionbar_Post_Cover;
TextView actionbar_Title;
TextView actionbar_Subtitle;
TextView SongCurrentTime;
TextView SongDuration;
protected MediaPlayer mediaPlayer;
AudioManager audioManager;
int currentSec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.songs_single, null, false);
drawerLayout.addView(contentView, 0);
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.songs_single_header, lv,
false);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, numbers);
lv = (ListView) findViewById(R.id.Songs_Single_mahdi);
lv.addHeaderView(header);
lv.setAdapter(adapter);
final ImageView logo = (ImageView)findViewById(R.id.actionbar_logo);
logo.setVisibility(View.GONE);
cover = (ImageView)findViewById(R.id.Songs_Single_Cover);
actionbar_Post_Cover = (ImageView)findViewById(R.id.actionbar_Post_Cover);
actionbar_Post_Cover.setVisibility(View.VISIBLE);
actionbar_Title = (TextView) findViewById(R.id.actionbar_Title);
actionbar_Subtitle = (TextView) findViewById(R.id.actionbar_Subtitle);
actionbar_Subtitle.setVisibility(View.VISIBLE);
playBtn = (ImageView)findViewById(R.id.Songs_Single_Controls_Controls_PlayBtn);
nextBtn = (ImageView)findViewById(R.id.Songs_Single_Controls_Controls_NextBtn);
prevBtn = (ImageView)findViewById(R.id.Songs_Single_Controls_Controls_PrevBtn);
songSeekBar = (SeekBar)findViewById(R.id.Songs_Single_Controls_SeekBar_SeekBar);
volumeSeekBar = (SeekBar)findViewById(R.id.Songs_Single_Controls_VolumeBar_VolumeBar);
SongCurrentTime = (TextView) findViewById(R.id.Songs_Single_Controls_SongCurrentTime);
SongDuration = (TextView) findViewById(R.id.Songs_Single_Controls_SongDuration);
if (Values.coverUrl != ""){
Picasso.with(getApplicationContext()).load(Values.coverUrl).into(cover);
Picasso.with(getApplicationContext()).load(Values.coverUrl).into(actionbar_Post_Cover);
}
if (Values.postLikes != "")
actionbar_Subtitle.setText(Values.postLikes+" Likes | "+Values.postDislikes+" Dislikes | "+Values.postViews+" Plays" );
if ((Values.postTitle != "") && (Values.songName != ""))
actionbar_Title.setText(Values.postTitle+" - "+Values.songName);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volumeSeekBar.setMax(maxVolume);
volumeSeekBar.setProgress(currentVolume);
volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mediaPlayer.setDataSource(Values.songPlayerUrl);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Error playing song.",Toast.LENGTH_LONG).show();
}
mediaPlayer.start();
int durationMin = ((mediaPlayer.getDuration())/1000) / 60;
int durationSec = ((mediaPlayer.getDuration())/1000) % 60;
final int currentMin = ((mediaPlayer.getCurrentPosition())/1000) / 60;
currentSec = ((mediaPlayer.getCurrentPosition())/1000) % 60;
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
SongCurrentTime.setText(currentMin+ " : "+currentSec);
}
});
}
} catch (InterruptedException e) {
//
}
}
};
t.start();
SongDuration.setText(durationMin+ " : "+durationSec);
}
});
}
}
答案 0 :(得分:1)
问题可能正在发生,因为您没有停止在Songs_Single活动中创建的媒体播放器,然后当您进入主要活动时,您正在创建另一个Media Player实例并播放另一首歌曲,它将同时播放两个媒体播放器同时进行。
要解决此问题,您必须使用相同的MediaPlayer
在后台管理您的歌曲,您可以使用Servive
或Intent Service
来执行此操作。
有关服务的工作,请阅读此answer
答案 1 :(得分:0)
mp
是您的媒体播放器。
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}