2011-06-18 4 views
2

je suis en train de lire un fichier son sur l'émulateur sur clic de bouton, mais je reçois un message « application audio de lecture est arrêté de façon inattendue »comment jouer un son sur un clic de bouton

mes codes sont les suivants:

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

accepter la réponse selon ce qui vous a aidé pour qu'il serait utile aux autres et aussi augmenterait votre réputation. @akash gupta – Lavanya

Répondre

2

Déplacez la création de l'instance MediaPlayer vers la méthode onCreate. Cela fera fonctionner votre application:

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

En outre, si vous essayez de démarrer une nouvelle instance de votre activité sur le bouton cliquez sur Je crois que c'est la bonne façon de le faire:

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

Essayez ceci:

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() devrait être avant de commencer une nouvelle activité.

 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(); 
      } 
     } 
    }); 
Questions connexes