Playing multiple songs with MediaPlayer at the same time: only one is really playing

前端 未结 5 1868
甜味超标
甜味超标 2021-01-02 04:44

I need help with playing multiple audio tracks at the same time in Android.

I am supposed to play three audio tracks at the exact same

5条回答
  •  时光说笑
    2021-01-02 05:01

    hi try below code it will work it the song wont stop you just need to select the song from spinner and click on start button, then select next song from spinner and click start it will work.

    public class MainActivity extends AppCompatActivity {
    
        Spinner sp;
        Button bstart;
        MediaPlayer mp;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            sp=(Spinner)findViewById(R.id.sp);
            String [] mys={"demo","democheap","demorehana"};
            ArrayAdapter data=new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,mys);
            sp.setAdapter(data);
    
        }
    
        public void code(View v)
        {
            if(sp.getSelectedItem().toString().equals("demo"))
            {
                mp=MediaPlayer.create(this,R.raw.demo);
            }
            else if(sp.getSelectedItem().toString().equals("democheap"))
            {
                mp=MediaPlayer.create(this,R.raw.democheap);
            }
            else if(sp.getSelectedItem().toString().equals("demorehana"))
            {
                mp=MediaPlayer.create(this,R.raw.demorehana);
            }
    
            if(v.getId()==R.id.bstart)
            {
                mp.start();
            }
        }
    }
    

提交回复
热议问题