gpt4 book ai didi

java - Android MediaPlayer无法正确播放音频

转载 作者:行者123 更新时间:2023-12-03 02:14:09 26 4
gpt4 key购买 nike

我的应用程序正在播放Raw文件夹中的5首不同的歌曲,我使用列表 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错误消息
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)

最佳答案

请参阅我在这个问题上的回答:Problems with Media Player

通常,您可能需要检查每首歌曲是否单独播放。如果是这样,您应该停止它。问题可能出在您检查其他任何媒体播放器是否正在播放,并暂停所有媒体而与停止哪个媒体播放器无关的方式上。

也就是说,您应该检查一个,如果正在播放,则停止它,一个接一个,就像我链接的那篇文章一样。

关于java - Android MediaPlayer无法正确播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21375278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com