2012-08-22 106 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