2014-10-23 2 views
0

버튼을 누르면 사운드 파일이 재생되기 시작합니다. 이미 원시 폴더를 만들고 거기에 .wav 파일을 넣었 으면합니다. 문제는이 일을하는 법을 모른다는 것입니다. 나는 비슷한 질문을 여기에서 확인했지만 그들은 내 문제를 해결하지 않는 것 같습니다. 어떻게해야합니까? 미리 감사드립니다. 여기android studio에서 버튼 클릭시 소리가 나는 이유는 무엇입니까?

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Button; 
import android.media.MediaPlayer; 
import android.view.View.OnClickListener;  

public class MyActivity extends ActionBarActivity {  

int clicked= 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 

    //start 


    //end 

    final TextView text = (TextView) findViewById(R.id.textView); 
    text.setText("Score: 0"); 

    final Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      clicked++; 
text.setText("Score: " + clicked);  
     } 
    }); 
     } 


     @Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.my, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
    } 

는 XML 코드 :

<ImageView 
    android:layout_width="100dp" 
    android:layout_height="173dp" 
    android:id="@+id/imageView" 
    android:background="@drawable/aidsv" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="70dp" 
    android:adjustViewBounds="false" /> 

<Button 
    android:layout_width="200dp" 
    android:layout_height="29dp" 
    android:text="Click me!" 
    android:id="@+id/button" 
    android:background="@drawable/redbottone" 
    android:layout_marginTop="39dp" 
    android:layout_below="@+id/imageView" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:layout_width="120dp" 
    android:layout_height="42dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Score: " 
    android:id="@+id/textView" 
    android:textColor="#FF0000" 
    android:background="@drawable/sfondotempo" 
    android:autoText="false" 
    android:layout_below="@+id/button" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="39dp" /> 

답변

5
 Log.v(TAG, "Initializing sounds..."); 

     final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound); 

     Button play_button = (Button)this.findViewById(R.id.button); 
     play_button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Log.v(TAG, "Playing sound..."); 
       mp.start(); 
      } 
     }); 

장소 고해상도의 사운드/원시 폴더

여기 내 코드입니다.

+0

어디에서이 코드를 추가해야합니까? – xManfred

+0

u 시작/끝 태그 사이에 아무 곳이나 추가 할 수 있습니다. – Soheyl

+1

네, 거기에 넣으려고했지만 작동하지 않습니다. – xManfred

관련 문제