我的应用程序正在播放原始文件夹中的5首不同的歌曲,我使用list-View和数组适配器列出了歌曲。我的代码的问题是,当我播放任何歌曲时,它可以正常工作,然后我从列表中播放另一首歌,但是当我想播放第三首歌时,它将无法播放,但我之前播放的前两首歌曲可以播放和暂停。我以许多不同的方式尝试过这些问题,但都是徒劳的。下面是我包含的代码。
package khan.panizai.adele21;
import java.util.List;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MusicActivity extends ListActivity {
MediaPlayer s1,s2,s3,s4,s5;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_layout);
Log.d("Saoud123", "music activity is called");
s1 = MediaPlayer.create(MusicActivity.this, R.raw.s1_rolling_in_the_deep);
s2 = MediaPlayer.create(MusicActivity.this, R.raw.s2_turning_tabels);
s3 = MediaPlayer.create(MusicActivity.this, R.raw.s3_dont_you_remember);
s4 = MediaPlayer.create(MusicActivity.this, R.raw.s4_set_fire_to_the_rain);
s5 = MediaPlayer.create(MusicActivity.this, R.raw.s5_someone_like_you);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.songs_list)));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
if(getListAdapter().getItem(position).equals("Rolling in the deep")){
//stop other songs and play this
if(s2.isPlaying()||s3.isPlaying()||s4.isPlaying()||s5.isPlaying()){
s2.pause();s3.pause();s4.pause();s5.pause();
s2.seekTo(0);s3.seekTo(0);s4.seekTo(0);s5.seekTo(0);
}
s1.start();
s1.setLooping(true);
}
else if(getListAdapter().getItem(position).equals("Turning tables")){
//stop other songs and play this
if(s1.isPlaying()||s3.isPlaying()||s4.isPlaying()||s5.isPlaying()){
s1.pause();s3.pause();s4.pause();s5.pause();
s1.seekTo(0);s3.seekTo(0);s4.seekTo(0);s5.seekTo(0);
}
s2.start();
s2.setLooping(true);
}
else if(getListAdapter().getItem(position).equals("Dont you remember")){
//stop other songs and play this
if(s2.isPlaying()||s1.isPlaying()||s4.isPlaying()||s5.isPlaying()){
s2.pause();s1.pause();s4.pause();s5.pause();
s2.seekTo(0);s1.seekTo(0);s4.seekTo(0);s5.seekTo(0);
}
s3.start();
s3.setLooping(true);
}
else if(getListAdapter().getItem(position).equals("Set fire to the rain")){
//stop other songs and play this
if(s2.isPlaying()||s3.isPlaying()||s1.isPlaying()||s5.isPlaying()){
s2.pause();s3.pause();s1.pause();s5.pause();
s2.seekTo(0);s3.seekTo(0);s1.seekTo(0);s5.seekTo(0);
}
s4.start();
s4.setLooping(true);
}
else if(getListAdapter().getItem(position).equals("Someone like you")){
//stop other songs and play this
if(s2.isPlaying()||s3.isPlaying()||s4.isPlaying()||s1.isPlaying()){
s2.pause();s3.pause();s4.pause();s1.pause();
s2.seekTo(0);s3.seekTo(0);s4.seekTo(0);s1.seekTo(0);
}
s5.start();
s5.setLooping(true);
}
}
@Override
public void onBackPressed() {
if(s1.isPlaying()||s2.isPlaying()||s3.isPlaying()||s4.isPlaying()
||s5.isPlaying())
s1.stop();s2.stop();s3.stop();s4.stop();s5.stop();
super.onBackPressed();
}
@Override
protected void onDestroy() {
if(s1.isPlaying()||s2.isPlaying()||s3.isPlaying()||s4.isPlaying()
||s5.isPlaying())
s1.release();s2.release();s3.release();s4.release();s5.release();
super.onDestroy();
}
}
这里是logcat错误消息msgs
01-31 19:15:19.485: D/AbsListView(9369): setCacheColorHint current in default theme.
01-31 19:15:19.485: D/Saoud123(9369): music activity is called
01-31 19:15:19.487: D/MediaPlayer(9369): mPlayerID = 128
01-31 19:15:19.509: D/MediaPlayer(9369): mPlayerID = 129
01-31 19:15:19.554: D/MediaPlayer(9369): mPlayerID = 130
01-31 19:15:19.590: D/MediaPlayer(9369): mPlayerID = 131
01-31 19:15:19.626: D/MediaPlayer(9369): mPlayerID = 132
01-31 19:15:20.336: I/MediaPlayer(9369): Duration update (duration=229392)
01-31 19:15:20.506: I/MediaPlayer(9369): Duration update (duration=250200)
01-31 19:15:20.556: I/MediaPlayer(9369): Duration update (duration=243288)
01-31 19:15:20.656: I/MediaPlayer(9369): Duration update (duration=241776)
01-31 19:15:20.663: I/MediaPlayer(9369): Duration update (duration=287136)
01-31 19:15:51.687: E/MediaPlayer(9369): pause called in state 8
01-31 19:15:51.688: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:51.688: E/MediaPlayer(9369): pause called in state 8
01-31 19:15:51.688: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:51.692: E/MediaPlayer(9369): pause called in state 8
01-31 19:15:51.692: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:51.692: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state:
mPlayer=0x21a800, mCurrentState=0
01-31 19:15:51.692: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:51.692: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state:
mPlayer=0x2a4a10, mCurrentState=0
01-31 19:15:51.692: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:51.693: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state: mPlayer=0x295ea0, mCurrentState=0
01-31 19:15:51.693: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:51.709: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:59.592: E/MediaPlayer(9369): pause called in state 0
01-31 19:15:59.592: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:59.592: E/MediaPlayer(9369): pause called in state 0
01-31 19:15:59.592: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:59.593: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state:
mPlayer=0x2a4a10, mCurrentState=0
01-31 19:15:59.594: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:59.594: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state:
mPlayer=0x295ea0, mCurrentState=0
01-31 19:15:59.594: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:59.594: E/MediaPlayer(9369): start called in state 0
01-31 19:15:59.594: E/MediaPlayer(9369): error (-38, 0)
01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)
01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)
答案 0 :(得分:0)
请参阅此问题中的答案:Problems with Media Player
大多数情况下,您可能需要检查每首歌曲是否正在单独播放 。如果是的话,你应该阻止它。问题可能在于您检查是否有其他媒体播放器正在播放,并暂停所有,无论哪个媒体播放器停止播放。
也就是说,你应该检查一个,如果它正在播放则停止它,一个接一个地就像在我链接的帖子中一样。