문제

I need to create a very simple instrument app.Just 7 buttons and 7 notes , a simple piano.I downloaded 7 different short wav files.I need to play them by clicking on button. And when I release the button the sound should stop. I know that it should have been easy but the samples I tried didnt work. There are lots of soundpool and mediaplayer code examples but when I tried them they didnt work and I am not sure if this is because of the soundfiles I downloaded or something else. During my research I found this example

Playing short .wav files - Android

But it didnt help. I already downloaded notes from this website

https://www.freesound.org/people/pinkyfinger/sounds/68448/

and uploaded them to raw folder. How can I play them?

According to the links this is the updated code but it stops playing after about 20 clicks.

package com.example.playaudio;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


    public class MainActivity extends Activity implements OnClickListener{
        private MediaPlayer mp;
        private MediaPlayer mp2;
        private MediaPlayer mp3;
        private MediaPlayer mp4;
        private MediaPlayer mp5;
        private MediaPlayer mp6;
        private MediaPlayer mp7;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setVolumeControlStream(AudioManager.STREAM_MUSIC);
            Button button1=(Button)findViewById(R.id.button_1);
            Button button2=(Button)findViewById(R.id.button_2);
            Button button3=(Button)findViewById(R.id.button_3);
            Button button4=(Button)findViewById(R.id.button_4);
            Button button5=(Button)findViewById(R.id.button_5);
            Button button6=(Button)findViewById(R.id.button_6);
            Button button7=(Button)findViewById(R.id.button_7);

            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
            button3.setOnClickListener(this);
            button4.setOnClickListener(this);
            button5.setOnClickListener(this);
            button6.setOnClickListener(this);
            button7.setOnClickListener(this);

        }

        public void onClick(View v) {
            int resId;
            int resId2;
            int resId3;
            int resId4;
            int resId5;
            int resId6;
            int resId7;


            switch (v.getId()) {
            case R.id.button_1:
                resId = R.raw.a;
                mp = MediaPlayer.create(this, resId);

                mp.start();
                break;
            case R.id.button_2:
                resId2 = R.raw.b;
                mp2 = MediaPlayer.create(this, resId2);
                mp2.start();
                break;
            case R.id.button_3:
                resId3 = R.raw.c;
                mp3 = MediaPlayer.create(this, resId3);
                mp3.start();
                break;
            case R.id.button_4:
                resId4 = R.raw.d;
                mp4 = MediaPlayer.create(this, resId4);
                mp4.start();
                break;
            case R.id.button_5:
                resId5 = R.raw.e;
                mp5 = MediaPlayer.create(this, resId5);
                mp5.start();
                break;
            case R.id.button_6:
                resId6 = R.raw.f;
                mp6 = MediaPlayer.create(this, resId6);
                mp6.start();
                break;
            case R.id.button_7:
                resId7 = R.raw.p;
                mp7 = MediaPlayer.create(this, resId7);
                mp7.start();
                break;

            }


        }

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



    }
도움이 되었습니까?

해결책

For a button click use this tutorial Sound Buttons

Or this one Playing audio

They are both helpful and should be what your looking for.

다른 팁

Create and initialise all your media players beforehand (not on the OnClick). In the OnClick just start playing. Something like

    mpx= new MediaPlayer();
    mpx.reset();
    mpx.setVolume(m_volume, m_volume);    
    mpx.setDataSource(YourMediaSource);
    mpx.prepare();

And in the OnClick()

mpx.start();

And yes you can play more than one button together. For that you already do what you need: use a separate player for every note. The tricky part is going to be handling lengths of your sounds... I am not sure but can try in the OnClck something like:

    if (!mpx.isPlaying())
        mpx.start();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top