0

我正在尝试使用媒体播放器运行 mp3 文件。它编译得很好,但它没有播放 mp3 文件。即使我检查了 isPlaying(),它也返回 false。请告诉我它有什么问题。这是代码:

package com.example.soundplayer;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    /**
     * Variables
     */
    MediaPlayer mp = null;
    String hello = "Hello!";
    String goodbye = "GoodBye!";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * Talking with the buttonHello
         */
        final Button buttonHello = (Button) findViewById(R.id.idHello);
        buttonHello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                managerOfSound(hello);
            } // END onClick()
        }); // END buttonHello
    }     
    /**
     * Manager of Sounds
     */
    protected void managerOfSound(String theText) {
        mp = MediaPlayer.create(MainActivity.this, R.raw.sound);
        mp = new MediaPlayer();
        if (theText.equals(hello))
        {
            MediaPlayer.create(this, R.raw.sound);
            mp.setVolume(1.0F, 1.0F);
            mp.reset();
            try {
                mp.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (mp != null) {
                mp.start();
            }
            if(mp.isPlaying()== true){
                Toast.makeText(this, "mp is playing ", Toast.LENGTH_LONG).show();}
        }
    }
}
4

1 回答 1

1

试试下面的代码

protected void managerOfSound(String theText) {
    mp = MediaPlayer.create(MainActivity.this, R.raw.sound);
    mp.setVolume(1.0F, 1.0F);
    if (theText.equals(hello))
    {            
        if (mp != null) {
            mp.start();
        }
        if(mp.isPlaying()== true){
            Toast.makeText(this, "mp is playing ", Toast.LENGTH_LONG).show();}
    }
}
于 2012-08-22T09:17:04.733 回答