为什么我的Android MP3播放器代码不起作用?

时间:2013-04-29 13:28:39

标签: java android eclipse media-player mp3

我正在尝试制作一个简单的Android MP3播放器,它将播放由mediaplayer = mediaplayer.create代码行声明的MP3!

XML代码是一个简单的按钮,表示播放!

我希望按下按钮时播放MP3 ...

有人可以帮忙吗?

import android.R;  //This automatically generates R file (references resources)
import android.app.Activity;
import android.media.MediaPlayer; - //This allows for predefined MP3 start/stop classes      to be used
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener; 
import android.widget.Button;

public class Mp3player extends Activity {

private Button buttonPlayStop;
private MediaPlayer mediaPlayer;


private final Handler handler = new Handler();


@Override
public void onCreate(Bundle icicle) { //Place holder for ‘OnFreeze’ parameter
        super.onCreate(icicle);
        setContentView(R.layout.activity_main); //Referencing XML file
        initViews();  

}

// setOnClickListener method (buttonClick())
private void initViews() {
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(new OnClickListener() {@Override public void   onClick(View v) {buttonClick();}});

    mediaPlayer = MediaPlayer.create(this, R.raw.music); // Sound file location


// event handler for buttonClick event
private void buttonClick(){ //When button is clicked do…
    if (buttonPlayStop.getText() == getString(R.string.play_str)) {
        buttonPlayStop.setText(getString(R.string.pause_str)); //
        try{
            mediaPlayer.start(); //Play MP3
        }
    }else {
        buttonPlayStop.setText(getString(R.string.play_str));
        mediaPlayer.pause(); //Pause MP3
    }
}
}

2 个答案:

答案 0 :(得分:1)

首先,检查按钮标题媒体播放器是否正在播放并不是一个好主意 - 而且你做错了。 String.equals()检查字符串中的相等性,而==检查字符串引用的相等性。

private void buttonClick(){ //When button is clicked do…
    if( !mediaPlayer.isPlaying() ) {
        buttonPlayStop.setText( getString(R.string.pause_str) );
        try {
            mediaPlayer.start(); // Play MP3
        } catch ( Exception e ) {
            // Handle Exception
        }
    } else {
        buttonPlayStop.setText( getString(R.string.play_str ) );
        mediaPlayer.pause();
    }
}

您可能还想设置onCompletionListener

class PlayerActivity implements MediaPlayer.OnCompletionListener {

void onCompletion( MediaPlayer mp ) {
    buttonPlayStop.setText( getString(R.string.play_str ) );
}

和您的initViews()

mediaPlayer.setOnCompletionListener( this );

答案 1 :(得分:0)

package com.GuitarPrep1;

import android.R;  //This automatically generates R file (references resources)
import android.app.Activity;
import android.media.MediaPlayer; - //This allows for predefined MP3 start/stop classes      to be used
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener; 
import android.widget.Button;

public class Mp3player extends Activity {

private Button buttonPlayStop;
private MediaPlayer mediaPlayer;


private final Handler handler = new Handler();


@Override
public void onCreate(Bundle icicle) { //Place holder for ‘OnFreeze’ parameter
        super.onCreate(icicle);
        setContentView(R.layout.activity_main); //Referencing XML file
        mediaPlayer = MediaPlayer.create(this, R.raw.music); // Sound file location
        initViews();  
        mediaPlayer.setOnCompletionListener (new MediaPlayer.OnCompletionListener(){

        @Override
        public void onCompletion(MediaPlayer arg0) {
        // TODO Auto-generated method stub
        buttonPlayStop.setText(getString(R.string.play_str));
        }});

}

// setOnClickListener method (buttonClick())
private void initViews() {
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(new OnClickListener() {@Override public void   onClick(View v) {buttonClick();}});




// event handler for buttonClick event
private void buttonClick(){ //When button is clicked do…      
    if(mediaPlayer!=null && mediaPlayer.isPlaying()){
             mediaPlayer.pause();
             buttonPlayStop.setText(getString(R.string.play_str));
    }else if(mediaPlayer!=null){
             mediaPlayer.start();
             buttonPlayStop.setText(getString(R.string.pause_str));
    }


}
}