2014-03-26 2 views
0

사운드 보드 앱을 만들려고하고 있지만 사운드 파일을 처리하는 데 문제가 있습니다. 좀 더 구체적으로 말하자면 버튼을 눌렀을 때 해당 사운드를 재생할 수있는 방법을 모릅니다.Android 버튼 사운드 보드

나는 각각의 사운드 파일 및 버튼 만 시작까지의 응용 프로그램 충돌을위한 미디어 플레이어를 구현하는 노력이 방법 Play sound on button click android

를 사용하여 시도했습니다.

I가/

그것이 작동하는 방법에 어떤 생각 원시 (10)는 SRC/고해상도의 원시 파일에있는 사운드 파일을 해당하는 10 개 버튼 /?

//this is a breaking bad soundboard app, so please excuse the language. Sorry if you  find it offensive 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()) 
       .commit(); 
    } 

    whatup = (Button)this.findViewById(R.id.button); 
    haveatit = (Button)this.findViewById(R.id.button2); 
    hello = (Button)this.findViewById(R.id.button3); 
    whining = (Button)this.findViewById(R.id.button7); 
    money = (Button)this.findViewById(R.id.button4); 
    yeah= (Button)this.findViewById(R.id.button8); 
    miserable = (Button)this.findViewById(R.id.button5); 
    mother = (Button)this.findViewById(R.id.button9); 
    gatorade = (Button)this.findViewById(R.id.button6); 
    science = (Button)this.findViewById(R.id.button10); 

    whatup.setOnClickListener(this); 
    haveatit.setOnClickListener(this); 
    hello.setOnClickListener(this); 
    whining.setOnClickListener(this); 
    money.setOnClickListener(this); 
    yeah.setOnClickListener(this); 
    miserable.setOnClickListener(this); 
    mother.setOnClickListener(this); 
    gatorade.setOnClickListener(this); 
    science.setOnClickListener(this); 

    WhatUp = MediaPlayer.create(MainActivity.this, R.raw.whatupb); 
    HaveAtIt = MediaPlayer.create(MainActivity.this, R.raw.haveatit); 
    Hello = MediaPlayer.create(MainActivity.this, R.raw.hello); 
    Whining = MediaPlayer.create(MainActivity.this, R.raw.stopwhining); 
    Money = MediaPlayer.create(MainActivity.this, R.raw.wheresmymoney); 
    Yeah = MediaPlayer.create(MainActivity.this, R.raw.yeahb); 
    Miserable = MediaPlayer.create(MainActivity.this, R.raw.miserableb); 
    Mother = MediaPlayer.create(MainActivity.this, R.raw.motherofgod); 
    Gatorade = MediaPlayer.create(MainActivity.this, R.raw.gatorade); 
    Science = MediaPlayer.create(MainActivity.this, R.raw.yeahscience); 

} 

이 버튼을 누르면 처리됩니다. 분명히 더 있어야 할 필요가 있지만 버튼을 테스트하는 중이었고 시작하려고 할 때 충돌이 발생합니다.

@Override 
public void onClick(View view) { 
    if(view==whatup) 
    { 
     WhatUp.start(); 
    } 
} 

로그 고양이 오류 : http://imgur.com/ZWYsLl7

답변

0

나는 당신의 소리 등 sound_1, sound_2, sound_3, 수 있습니다 가정하고 버튼을 button_1, button_2이다 등등 ..
당신은 루프를 만들어야합니다 onCreate 방법으로 ID를 얻는 방법 :

for (int i = 0; i < 10; i++) { 
    // get the id for buttons 
    int btnId = getResources().getIdentifier("button_"+i, "id", getPackageName()); 
    // get the res for sounds 
    int rawId = getResources().getIdentifier("sound_"+i, "raw", getPackageName()); 
    // set a click listener to all your buttons 
    button[i] = (Button) findViewById(id); 
    button[i].setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
      // create the media player with the raw id 
      mp = MediaPlayer.create(MainActivity.this, rawId); 
      mp.setOnCompletionListener(new OnCompletionListener() { 
       @Override 
       public void onCompletion(MediaPlayer mp) { 
        // TODO Auto-generated method stub 
        // avoid NullPointerException see the links below 
        mp.release(); 
       } 
      }); 
      // play the sound 
      mp.start(); 
     } 
    }); 
} 

톱 c 참조 : Using MediaPlayer. 문맥 : How to dynamically generate the raw resource identifier in android?과 함께 식별자를 얻을 수도 있는데, release() 방법의 설명에 도움이 될 수 있습니다 : Play sound on button click - Null pointer exception 그리고 Releasing the MediaPlayer 부분을 Using MediaPlayer에서 읽어보십시오. 나는 잘 모르겠지만 그 트릭을해야합니다 ..
행운을 빌어 요.


UPDATE

변경이 체크 if(view.getId() == R.id.button)-if(view==whatup).
onClick 방법에서는보기에 관한 ID를 getId() 방법으로 확인해야합니다.

+0

도움이 될 것 같습니다. 방금 미디어 플레이어 대신 사운드 풀을 사용해야한다는 기사를 발견했습니다. 차이점을 알고 있습니까? – Programatic

+0

저는 방금 반대 의견을 읽었습니다 : http://www.stealthcopter.com/blog/2010/08/android-soundpool-vs-mediaplayer-focus-on-soundboards-and-memory-problems/ -Awesome! ^^ – Fllo

+0

나는 그것을 뒤집었고 시작시 여전히 충돌이 난다. 다음은 onCreate에 대한 코드입니다. – Programatic