2012-08-22 72 views
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();} 
     } 
    } 
} 

回答

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();} 
    } 
} 
+1

感谢先生及其工作 –

+0

你[R欢迎:) – Braj