我认为我的问题描述了自己, 无论如何这里是代码
public class NowPlaying extends Activity implements Serializable {
static MediaPlayer mp =new MediaPlayer();
String artist;ProgressDialog pd;
int position; String key=null ;
/*int z=0;
SeekBar songProgressBar ;
TextView songCurrentDurationLabel;
TextView songTotalDurationLabel ;
private Handler mHandler = new Handler();
private Utilities utils=new Utilities();
*/
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.now_playing);
Intent i = getIntent();
position=i.getIntExtra("Data2", 0);
/* songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
*/
final ArrayList<SongDetails> songs = getIntent().getParcelableArrayListExtra("Data1");
pd = new ProgressDialog(this);
pd.setMessage("Loading...");
Playservice(songs,position );
Button bfake=(Button) findViewById(R.id.bFake);
LinearLayout LL=(LinearLayout) findViewById(R.id.LL);
Button Pause=(Button)findViewById(R.id.bPlayPause);
// songProgressBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) this); // Important
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer player) {
position=position+1;
Playservice(songs,position);
}});
bfake.setOnTouchListener(new OnSwipeTouchListener()
{ public void onSwipeRight() {
position=position-1;
Playservice(songs,position );
}
public void onSwipeLeft() {
position=position+1;
Playservice(songs,position );
}
public void onSwipeBottom() {
mp.stop();
}
});
LL.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeBottom() {
mp.stop();
}
});
Pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{if(mp.isPlaying())
mp.pause();
else
mp.start();
}
});
}
private void Playservice( ArrayList<SongDetails> songs, int position )
{
int currentPosition= 0;
int total = mp.getDuration();
/* while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
break;
} catch (Exception e) {
break;
}
// songProgressBar.setProgress(currentPosition);
*/
try {
String ab=songs.get(position).getPath2().toString();
artist=songs.get(position).getArtist();
new Xmlparse().execute();
if(mp.isPlaying())
{ mp.stop();
}
mp.reset();
mp.setDataSource(ab) ;
mp.prepare();
mp.start();
// songProgressBar.setProgress(0);
// songProgressBar.setMax(mp.getDuration());
new Thread().start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/*public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser) mp.seekTo(progress);
}
*/
class Xmlparse extends AsyncTask<Void,Drawable,Drawable>
{
protected void onPostExecute(Drawable result) {
// super.onPostExecute(result);
pd.dismiss();
LinearLayout LL=(LinearLayout) findViewById(R.id.LL);
LL.setBackgroundDrawable(result);
// Toast.makeText(getApplicationContext(),result, 1000).show();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.show();
}
@Override
protected Drawable doInBackground(Void... params) {
Drawable s =getData();
return s;
}
public Drawable getData()
{ Drawable drawable = null;//=new BitmapDrawable(bmp);
artist=artist.trim();
artist=artist.replace(" ", "%20");
try{
URL url = new URL("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" +artist+ "&api_key=1732077d6772048ccc671c754061cb18");
URLConnection connection = url.openConnection();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
final Document document = db.parse(connection.getInputStream());
document.getDocumentElement().normalize();
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xPathEvaluator = xPathfactory.newXPath();
XPathExpression nameExpr = xPathEvaluator.compile("//lfm/artist/image");
// XPathExpression nameExpr = xPathEvaluator.compile("//lfm/tracks/track/image");
NodeList nl = (NodeList) nameExpr.evaluate(document, XPathConstants.NODESET);
for (int zzz = 0; zzz < nl.getLength(); zzz++)
{
Node currentItem = nl.item(zzz);
key = currentItem.getTextContent();
// key = currentItem.getAttributes().getNamedItem("uri").getNodeValue();
}URL ulrn = new URL(key);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
drawable=new BitmapDrawable(bmp);
}
catch(Exception e)
{}
return drawable;}
}}
答案 0 :(得分:3)
重新启动,因为您的活动已重新创建...每次用户更改设备的方向时,都会再次调用oncreate方法。要解决这个问题,请尝试将此添加到清单文件中的活动中:
android:configChanges="orientation|keyboardHidden">
答案 1 :(得分:3)
在你的清单中试试这个......
<activity android:name="Your Activity Name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>