0

我正在尝试制作一个简单的 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
    }
}
}
4

2 回答 2

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 );
于 2013-04-29T13:54:00.703 回答
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));
    }


}
}
于 2013-04-29T14:41:27.960 回答