代码的主要部分:
// This is event handler for buttonClick event
void buttonClick(){
if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str));
try{
mediaPlayer.start();
startPlayProgressUpdater();
}catch (IllegalStateException e) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
mediaPlayer.pause();
mediaPlayer.seekTo(0);
}
}else {
buttonPlayStop.setText(getString(R.string.play_str));
mediaPlayer.pause();
}
这些错误,暂停调用状态8错误(-38,0)错误(-38,0)启动调用状态0错误(-38,0)错误(-38,0)并且歌曲是不玩了。
答案 0 :(得分:0)
当您调用其功能时,Media Player有自己的生命周期,确保您处于媒体播放器的正确状态
最好的方法是制作媒体播放器服务并绑定到您活动中的该服务,并维护该服务中的所有媒体播放器方法。
http://examples.javacodegeeks.com/android/android-mediaplayer-example/
答案 1 :(得分:0)
在玩家活动中添加这些
public MusicService musicSrv ;
private boolean musicBound=false;
private ArrayList<Song> songList;
在oncreate里面
songList = new ArrayList<Song>();
getSongList();
在你的行为能力中添加这个 @覆盖 protected void onStart(){ super.onStart();
if(playIntent==null){
playIntent = new Intent(this, MusicService.class);
this.bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
this.startService(playIntent);
}
}
private ServiceConnection musicConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.MusicBinder binder = (MusicService.MusicBinder)service;
//get service
musicSrv = binder.getService();
//pass list
musicSrv.setList(songList);
musicBound = true;
songTitleLabel.setText(""+musicSrv.getSongTitle());
}
@Override
public void onServiceDisconnected(ComponentName name) {
musicBound = false;
}
};
public void getSongList(){
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if(musicCursor!=null && musicCursor.moveToFirst()){
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
songList.add(new Song(thisId, thisTitle, thisArtist));
}
while (musicCursor.moveToNext());
}
}
将列表添加到您的活动和列表项中,单击
if(musicSrv.isPlaying()){
if(musicSrv.status()!=null){
musicSrv.pauseSong();
btnPlay.setImageResource(R.drawable.btn_play);
}
}else{
if(musicSrv.status()!=null){
musicSrv.restartSong();
btnPlay.setImageResource(R.drawable.btn_pause);
}
}
将此作为服务添加到您的应用中
public class MusicService extends Service实现MediaPlayer.OnPreparedListener,MediaPlayer.OnErrorListener,MediaPlayer.OnCompletionListener { 私人MediaPlayer播放器; 私人ArrayList歌曲; private int songPosn;
private final IBinder musicBind = new MusicBinder();
private boolean isRepeat=false;
private boolean isShuffle = false;
private int seekForwardTime = 5000;
private int seekBackwardTime = 5000;
private int currentSongIndex = 0;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return musicBind;
}
public void onCreate(){
super.onCreate();
songPosn=0;
player = new MediaPlayer();
initMusicPlayer();
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
}
public void initMusicPlayer(){
player.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
}
public void setList(ArrayList<Song> theSongs){
songs=theSongs;
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
public class MusicBinder extends Binder {
public MusicService getService() {
return MusicService.this;
}
}
public void playSong(){
try{
player.reset();
System.out.println(songs);
System.out.println(songs.get(songPosn));
Song playSong = songs.get(songPosn);
long currSong = playSong.getID();
Uri trackUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
currSong);
player.setDataSource(getApplicationContext(), trackUri);
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
try {
player.prepareAsync();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
public boolean isListNull()
{
if(songs==null)
return true;
else
return false;
}
public void setSong(int songIndex){
songPosn=songIndex;
}
public boolean isPlaying()
{
return player.isPlaying();
}
public void pauseSong()
{
player.pause();
}
public void restartSong()
{
player.start();
}
public String status()
{
if(player != null)
{
return "sdf";
}
else
return null;
}
public long getDuration()
{
return player.getDuration();
}
public long getCurrentPosition()
{
return player.getCurrentPosition();
}
public void seek(int currentPosition)
{
player.seekTo(currentPosition);
}
public MediaPlayer getPlayer()
{
return player;
}
public int getCurrentSongIndex()
{
System.out.println(songPosn);
return songPosn;
}
public String getSongTitle()
{
Song song = songs.get(songPosn);
return song.getTitle();
}
@Override
public void onCompletion(MediaPlayer arg0)
{
if(isRepeat){
setSong(songPosn);
playSong();
} else if(isShuffle){
// shuffle is on - play a random song
Random rand = new Random();
songPosn = rand.nextInt((songs.size() - 1) - 0 + 1) + 0;
setSong(songPosn);
playSong();
} else{
// no repeat or shuffle ON - play next song
if(songPosn < (songs.size() - 1)){
setSong(songPosn + 1);
playSong();
songPosn = songPosn + 1;
}else{
// play first song
setSong(0);
playSong();
songPosn = 0;
}
}
}
public void setRepeatSong(boolean re)
{
isRepeat = re;
}
public void setShuffelSong(boolean sh)
{
isShuffle = sh;
}
public void moveForward()
{
int currentPosition = (int) getCurrentPosition();
if(currentPosition + seekForwardTime <= getDuration()){
seek(currentPosition + seekForwardTime);
}else{
seek((int)getDuration());
}
}
public void moveBackward()
{
int currentPosition = (int) getCurrentPosition();
if(currentPosition - seekBackwardTime >= 0){
seek(currentPosition - seekBackwardTime);
}else{
seek(0);
}
}
public void moveNext()
{
currentSongIndex = getCurrentSongIndex();
if(currentSongIndex < (songs.size() - 1)){
setSong(currentSongIndex + 1);
playSong();
currentSongIndex = currentSongIndex + 1;
}else{
setSong(0);
playSong();
currentSongIndex = 0;
}
}
public void movePrevious()
{
currentSongIndex = getCurrentSongIndex();
if(currentSongIndex > 0){
setSong(currentSongIndex - 1);
playSong();
currentSongIndex = currentSongIndex - 1;
}else{
// play last song
setSong(songs.size() - 1);
playSong();
currentSongIndex = songs.size() - 1;
}
}
}
和你的清单中的这些
service android:name =“.Services.MusicService”
内部应用程序标记
使用这些权限
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
希望这对你有用!