我想一次运行多个媒体播放器

时间:2014-04-08 04:44:15

标签: android android-mediaplayer

我是Android开发的新手,正在开发一个我用于工作的应用程序。它需要多个按钮才能发出声音。然而,这比那更复杂。

我设法制作了一个媒体播放器,它将播放声音,暂停,淡入淡出等按钮,为按钮标签提供一个字符串,该字符串作为文件传递给播放器。停止并释放MP后,我可以按下其他按钮并开始播放新声音。我的问题是因为这是我的戏剧表演。我希望能够交叉混合(即一个声音淡出,下一个开始)。第一个想法是我需要为每个按钮使用不同的MP(这将使用大量的复制代码),而且我希望能够设置近100个按钮。

之前是否有人这样做过,我已经在网上搜索了几个小时以找到很少的帮助。任何帮助都会有用,提前谢谢。

我的代码在

下面
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Color;
    import android.media.MediaPlayer;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.os.Build;
    import android.widget.Button;
    import android.widget.TextView;
    import android.view.View.OnLongClickListener;
    import android.util.Log;
    import android.widget.Toast;

    import java.io.File;
    import java.io.IOException;
    import java.util.Timer;
    import java.util.TimerTask;

     public class MainActivity extends Activity  {

        MediaPlayer mp, mp2 ;

        Button Sound1 ,Sound2, Stop, Pause , Fade;
        TextView displaystatus;
        String bName = "button pressed";

    //set variables for volume control

    private int iVolume;
    private final static int INT_VOLUME_MAX = 100;
    private final static int INT_VOLUME_MIN = 0;
    private final static float FLOAT_VOLUME_MAX = 1;
    private final static float FLOAT_VOLUME_MIN = 0;




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

        //connect interface to local variables
        Fade=(Button)findViewById(R.id.bfade);
        Sound1 = (Button)findViewById(R.id.bSound1);
        Sound2 = (Button)findViewById(R.id.bSound2);
        Stop =(Button)findViewById(R.id.bStop);        Pause=(Button)findViewById(R.id.bPause);

        displaystatus=(TextView)findViewById(R.id.tStatus);


        mp2=new MediaPlayer();


        //Button clicks to make play/pause/stop
        Sound1.setOnClickListener(buttonPlayOnClickListener);
        Sound2.setOnClickListener(buttonPlayOnClickListener);
        Pause.setOnClickListener(buttonPauseOnClickListener);
        Stop.setOnClickListener( buttonQuitOnClickListener);
        Fade.setOnClickListener( buttonFadeOnClickListener);




        //set onlongclicklistener to open SoundDetailActivity

          Sound1.setOnLongClickListener(new View.OnLongClickListener(){

              public boolean onLongClick(View v) {
                  //get the tag for the button pressed

                  bName= v.getTag().toString();

                   whenLongClick();
                  return true;

              };
          });



        //set long click listener to open SoundDetailActivity

        Sound2.setOnLongClickListener(new View.OnLongClickListener(){

            public boolean onLongClick(View v) {

                //get the tag for the button pressed

               bName= v.getTag().toString();

                whenLongClick();
                return true;

            };
        });








    }



    private void initMediaPlayer ()
    {

        if(mp!=null){mp.stop();
        mp.release();}



        mp = new  MediaPlayer();




        File path=android.os.Environment.getExternalStorageDirectory();
        try {
            Log.v("paddy",path+bName);

            mp.setDataSource(path+bName );
            mp.prepare();

        }catch (IOException e){
            e.printStackTrace();
        }


    }

    Button.OnClickListener buttonPlayOnClickListener
            = new Button.OnClickListener(){



        @Override
        public void onClick(View v) {

            bName= v.getTag().toString();
            initMediaPlayer();

            Log.v("paddy",bName);

                   // if(mp.isPlaying()) {mp.reset();}

                        mp.start();
                        displaystatus.setText("- PLAYING -");
                        Pause.setText("Pause");
                        Log.v("paddy","no sound was playing");








        }
    };

    Button.OnClickListener buttonPauseOnClickListener
            = new Button.OnClickListener(){

        @Override
        public void onClick(View v) {



            // TODO Auto-generated method stub

            if(mp.isPlaying()) {

                mp.pause();
                displaystatus.setText("- resume -");
                Pause.setText("Resume");


            }else{

                mp.start();

                displaystatus.setText("- playing -");
                Pause.setText("Pause");


            }


            //finish();


        }

    };



    Button.OnClickListener buttonQuitOnClickListener
            = new Button.OnClickListener(){

        @Override
        public void onClick(View v) {



            // TODO Auto-generated method stub



            mp.stop();
            mp.reset();
            displaystatus.setText("- Ready -");



    }

    };

    public Button.OnClickListener buttonFadeOnClickListener
            =new Button.OnClickListener(){

        @Override
        public void onClick(View v) {

            fade(5000); ///time in milliseconds

            // TODO Auto-generated method stub
           // mp.stop();
            displaystatus.setText("- Fade out -");


            //finish();
        }


    };



    public void fade(int fadeDuration)
    {
        //Set current volume, depending on fade or not
        if (fadeDuration > 0)
            iVolume = INT_VOLUME_MAX;
        else
            iVolume = INT_VOLUME_MIN;

        updateVolume(0);

        //Start increasing volume in increments
        if(fadeDuration > 0)
        {
            final Timer timer = new Timer(true);
            TimerTask timerTask = new TimerTask()
            {
              public void run() {
                  MainActivity.this.runOnUiThread(new Runnable() {

                      @Override
                      public void run() {
                          updateVolume(-1);
                          if (iVolume <= INT_VOLUME_MIN) {
                              timer.cancel();
                              timer.purge();
                              //Pause music
                              if (mp.isPlaying()) mp.stop();

                              mp.release();

                              mp = new MediaPlayer();
                              mp = MediaPlayer.create(MainActivity.this, R.raw.franksinatra);

                              displaystatus.setText("- Ready -");
                                Log.v("paddy","getting to end of fade");
                          }
                      }
                  });
              }
            };

            // calculate delay, cannot be zero, set to 1 if zero
            int delay = fadeDuration/INT_VOLUME_MAX;
            if (delay == 0) delay = 1;

            timer.schedule(timerTask, delay, delay);
        }
    }


    // when a button is longclicked the activity sound details is opened and the sound button tag is sent as an extra.
     public void whenLongClick ()   {

         Toast.makeText(getApplicationContext(), bName , Toast.LENGTH_LONG).show();


         Intent i =  new  Intent(this,SoundDetailActivity.class);
                       i.putExtra("ButtonId",bName);
                       startActivity(i);

     }








    private void updateVolume(int change)
    {
        //increment or decrement depending on type of fade
        iVolume = iVolume + change;

        //ensure iVolume within boundaries
        if (iVolume < INT_VOLUME_MIN)
            iVolume = INT_VOLUME_MIN;
        else if (iVolume > INT_VOLUME_MAX)
            iVolume = INT_VOLUME_MAX;

        //convert to float value
        float fVolume = 1 - ((float) Math.log(INT_VOLUME_MAX - iVolume) / (float) Math.log(INT_VOLUME_MAX));

        //ensure fVolume within boundaries
        if (fVolume < FLOAT_VOLUME_MIN)
            fVolume = FLOAT_VOLUME_MIN;
        else if (fVolume > FLOAT_VOLUME_MAX)
            fVolume = FLOAT_VOLUME_MAX;

        mp.setVolume(fVolume, fVolume);
    }
}

0 个答案:

没有答案