我在android studio中创建了一个音乐应用程序。我得到的问题是,当我想在第一首歌的中间播放第二首歌曲时,它会播放第一首歌曲。第一首歌并没有停止播放。它继续播放第二首歌。
在应用程序中,我有两个活动为MainActivity.java有歌曲列表和NowPlaying.java播放歌曲。
下面给出了两个java类的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
String[] songList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateList();
}
private void populateList() {
songList = new String[]{"Jalma", "Demons", "Parelima", "Mero Balyakal Ko Sathi", "Audai Jadai",
"Case 420", "Euta Sathi", "Cheerleader"};
ListAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
songList);
ListView theList = (ListView) findViewById(R.id.theList);
theList.setAdapter(arrayAdapter);
theList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView theList = (ListView) findViewById(R.id.theList);
String selectedSong = (String) (theList.getItemAtPosition(position));
Intent toSecondActivity = new Intent(MainActivity.this, NowPlaying.class);
toSecondActivity.putExtra("playSong", selectedSong);
startActivity(toSecondActivity);
}
});
}
}
NowPlaying.java
public class NowPlaying extends AppCompatActivity {
MediaPlayer song;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_now_playing2);
song = MediaPlayer.create(this, R.raw.demons);
song.start();
song.pause();
selectSong();
pauseButton();
getSongList();
getNextButton();
getPreviousButton();
}
/**
* Method for selecting the song
* Moves to startSong method
*/
public void selectSong(){
checkIfSongIsPlaying();
String nextPlay = getIntent().getStringExtra("playSong");
TextView lyricsBox = (TextView) findViewById(R.id.lyricsBox);
lyricsBox.setMovementMethod(new ScrollingMovementMethod());
if (nextPlay.equals("Demons")) {
song = MediaPlayer.create(this, R.raw.demons);
lyricsBox.setText(R.string.demonsLyrics);
}
else if(nextPlay.equals("Jalma")){
song = MediaPlayer.create(this, R.raw.jalma);
lyricsBox.setText(R.string.jalmaLyrics);
}
else if(nextPlay.equals("Parelima")){
song = MediaPlayer.create(this, R.raw.parelima);
lyricsBox.setText(R.string.parelimaLyrics);
}
else if (nextPlay.equals("Case 420")) {
song = MediaPlayer.create(this, R.raw.case420);
lyricsBox.setText(R.string.case420Lyrics);
} else if (nextPlay.equals("Mero Balyakal Ko Sathi")) {
song = MediaPlayer.create(this, R.raw.balyakalsathi);
lyricsBox.setText(R.string.balyakalSathi);
} else if (nextPlay.equals("Euta Sathi")) {
song = MediaPlayer.create(this, R.raw.eutasathi);
lyricsBox.setText(R.string.eutaSathi);
} else if (nextPlay.equals("Audai Jadai")) {
song = MediaPlayer.create(this, R.raw.audaijadai);
lyricsBox.setText(R.string.audaijadaiLyrics);
} else if (nextPlay.equals("Cheerleader")) {
song = MediaPlayer.create(this, R.raw.cheerleader);
lyricsBox.setText(R.string.cheerleaderLyrics);
}
playSong();
}
public void checkIfSongIsPlaying(){
if(song !=null){
song.pause();
}
}
/**
* Method to start song (play the song)
*
*/
public void playSong(){
song.start();
getSongtitle();
getSeekbar();
onSongCompletion();
}
/**
* Method for pause Button
* to pause song once clicked and change button background to play image
* Again play the song if the button is pressed again. and change background back to pause image
*/
public void pauseButton(){
final Button playButton = (Button) findViewById(R.id.playButton);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (song.isPlaying()) {
playButton.setBackgroundResource(R.drawable.playbutton);
song.pause();
} else {
playButton.setBackgroundResource(R.drawable.pausebutton);
song.start();
}
}
});
}
/**
* Method to get the song title from first java file and display in the title
*/
public void getSongtitle(){
String nextPlay = getIntent().getStringExtra("playSong");
TextView songTitle = (TextView) findViewById(R.id.songTitle);
songTitle.setText(nextPlay);
}//end of getSongtitle method
/**
* Method for song list button
* Goes back to the first java file once the button is cliked,
* displays the song list
*/
public void getSongList(){
Button lyricsButton = (Button) findViewById(R.id.lyricsButton);
lyricsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(NowPlaying.this, MainActivity.class));
}
});
}
Runnable run = new Runnable() {
@Override
public void run() {
getSeekbar();
}
};
/**
* Method to update the seekbar.
* implement touch in seekbar to change song position
*/
public void getSeekbar(){
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(song.getDuration());
seekBar.setProgress(song.getCurrentPosition());
seekBar.postDelayed(run, 1000);
TextView endTimer = (TextView) findViewById(R.id.endTimer);
TextView startTimer = (TextView) findViewById(R.id.startTimer);
double finalTime = song.getDuration();
double startTime = song.getCurrentPosition();
double timeRemaining = finalTime - song.getCurrentPosition();
startTimer.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes((long)startTime ), TimeUnit.MILLISECONDS.toSeconds((long) startTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime))));
endTimer.setText(String.format("%d:%d", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int seek_to;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seek_to = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
song.seekTo(seek_to);
}
});
}
/**
* Lists the song list after song is completeted
*/
public void onSongCompletion(){
song.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer song) {
song.reset();
startActivity(new Intent(NowPlaying.this, MainActivity.class));
}
});
}
/**
* Method for next button
* the song skips every 10 seconds once clicked
*/
public void getNextButton(){
Button nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int startTime = song.getCurrentPosition();
int forwardTime = 10000;
startTime += forwardTime;
if (startTime <= song.getDuration()) {
song.seekTo(startTime);
} else {
song.stop();
}
}
});
} // end of getNextButton
/**
* Method for previous button
* the song skips back 10 seconds once clicked
*/
public void getPreviousButton(){
Button previousButton = (Button) findViewById(R.id.previousButton);
previousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int startTime = song.getCurrentPosition();
int previousTime = 10000;
startTime -= previousTime;
if (startTime >= 0) {
song.seekTo(startTime);
} else {
song.seekTo(0);
song.start();
}
}
});
} //end of getPreviousButton method
}
我想要的是,如果用户想播放第二首歌曲,则播放第一首歌曲。第一首要停止的歌曲和第二首要播放的歌曲。然而,在应用程序中,歌词,搜索栏,时间得到更新,但歌曲同时播放(彼此叠加)。
答案 0 :(得分:0)
为什么不写song.stop();而不是song.pause();在checkIfSongIsPlaying。你也可以写song.reset();在改变歌曲之前确定。
如果有效,请告诉我
答案 1 :(得分:0)
发生这种情况是因为每次点击NowPlaying
中的项目时都会创建listview
的新对象(这会创建新的MediaPlayer
个对象)。要解决此问题,您可以将NowPlaying
作为单身类。
另外,请查看您在onCreate
方法中调用的方法。在pause
之后立即调用start
方法毫无意义。
MainActivity.java
....
....
String selectedSong = (String) (theList.getItemAtPosition(position));
// instead of intents use the advantage of singleton class.
NowPlaying.getInstance(selectedSong);
....
....
NowPlaying.java
最好为此创建一个新的java文件,你可以使用get和set方法设置活动的值。
public class public class NowPlaying extends AppCompatActivity {
NowPlaying nowplaying;
String selectedSong;
MediaPlayer song;
public static NowPlaying getInstance(String selectedSong){
if(nowplaying == null){
nowplaying = new NowPlaying();
}
this.selectedSong = selectedSong;
return nowplaying;
}
将这些功能分开。
// these two methods are calling one after the other which won't allow the song to play completely.
song.start();
song.pause(); // keep this method in different method such as pause btn click listener.
.....
... rest of code ...
.....
}
希望,这有助于......!