我正在尝试Android中的音乐播放器,但它每次都会崩溃。我首先使用列表视图从哪里通过意图将数据发送到玩家活动。然后我和midea假设要播放音乐,但是出了什么问题呢?它甚至不开放。当我点击mainactivity类的歌曲列表时。它表明您的应用没有响应。这是我的播放器活动的java文件
package com.smp.ravirathore.materialdesign;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import java.io.File;
import java.util.ArrayList;
/**
* Created by Python on 9/28/2015.
*/
public class Player extends ActionBarActivity implements View.OnClickListener {
SeekBar seekBar;
Uri uri;
int position;
Button play, forward, backward, nxt, previous;
MediaPlayer player;
ArrayList<File> arrayList;
Thread seekupdate;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
Bundle b = i.getExtras();
arrayList = (ArrayList) b.getParcelableArrayList("mysongs");
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekupdate = new Thread() {
@Override
public void run() {
int totalDuration = player.getDuration();
int currentDuration = 0;
if (currentDuration < totalDuration) {
try {
sleep(500);
currentDuration = player.getCurrentPosition();
seekBar.setProgress(currentDuration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//super.run();
}
};
play = (Button) findViewById(R.id.play);
forward = (Button) findViewById(R.id.forward);
backward = (Button) findViewById(R.id.backwrd);
nxt = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
play.setOnClickListener(this);
forward.setOnClickListener(this);
backward.setOnClickListener(this);
nxt.setOnClickListener(this);
previous.setOnClickListener(this);
position = b.getInt("pos");
uri = Uri.parse(arrayList.get(position).toString());
player = MediaPlayer.create(getApplicationContext(), uri);
seekBar.setProgress(0);
player.start();
seekBar.setMax(player.getDuration());
seekupdate.start();
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
player.seekTo(seekBar.getProgress());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (player.isPlaying()) {
player.pause();
play.setText("Play");
} else {
player.start();
play.setText("Pause");
}
break;
case R.id.forward:
player.seekTo(player.getCurrentPosition() + 10000);
break;
case R.id.backwrd:
player.seekTo(player.getCurrentPosition() - 5000);
break;
case R.id.next:
player.stop();
player.release();
position = (position + 1) % arrayList.size();
uri = Uri.parse(arrayList.get(position).toString());
player = MediaPlayer.create(getApplicationContext(), uri);
seekBar.setProgress(0);
player.start();
seekBar.setMax(player.getDuration());
break;
case R.id.previous:
player.stop();
player.release();
position = (position - 1 < 0) ? (arrayList.size() - 1) : position - 1;
uri = Uri.parse(arrayList.get(position).toString());
player = MediaPlayer.create(getApplicationContext(), uri);
seekBar.setProgress(0);
player.start();
seekBar.setMax(player.getDuration());
break;
}
}
}