2011-06-18 93 views
2

我試圖發揮對按鈕的點擊模擬器的聲音文件,但我得到的消息「應用程序播放音頻意外停止」如何播放聲音按鈕的點擊

我的代碼是:

package com.java4u.android; 

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

public class p1 extends Activity { 


// creating instance of media player 
MediaPlayer mp=MediaPlayer.create(this,R.raw.meow); 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{  
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button btnSound=(Button)this.findViewById(R.id.playSound); 

    btnSound.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     startActivity(new Intent("com.java4u.android.p1")); 
      mp.start(); 
     } 
    }); 

     } 

} 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.java4u.android" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".p1" android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

的main.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button android:text="play audio" 
    android:id="@+id/playSound" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    </Button> 
</LinearLayout> 
+0

接受哪個幫你這樣,這將是幫助他人,也將提高你的聲譽答案。 @akash gupta – Lavanya

回答

2

MediaPlayer實例的創建移動到onCreate方法。 這將使您的應用程序運行:

// creating instance of media player 
MediaPlayer mp; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mp = MediaPlayer.create(this, R.raw.ljudman__grenade); 

另外,如果你想在啓動按鈕的活動的新實例點擊我相信這是做正確的方法:

startActivity(new Intent(p1.this, p1.class)); 
6

試試這個:

Button btn1=(Button)findViewById(R.id.xBtn1); 

btn1.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.fruit_dance); 
     mp1.start(); 
    } 
} 
2

mp.start()應該是開始一個新的活動之前。

 btnSound.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
      mp.start(); 
      startActivity(new Intent(currentActivityName.this, newActivityName.class)); 

      } 
     }); 
0
//inside toggle button 
    toggleButton = (ToggleButton)findViewById(R.id.sound); 
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.theme); 
    if(toggleButton.isChecked()) 
     mp.start(); 
    toggleButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(!toggleButton.isChecked()){ 
       mp.pause(); 
      } 
      else { 
       mp.start(); 
       mp.isLooping(); 
      } 
     } 
    }); 
相關問題