簡體   English   中英

Android MediaPlayer無法通過單擊另一個按鈕來停止

[英]Android MediaPlayer not stopping by clicking another button

當您單擊兩個按鈕時,您會聽到兩種聲音。 如何停止第一個聲音並播放第二個聲音,或者當我播放第一個聲音時第二個聲音停止? 我該如何創建呢? 這是我的媒體播放器

final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        JSONArray songs = (JSONArray) response.getJSONArray("songs");
                        for ( int i = 0; i < songs.length(); i++) 
                        {
                            // Create LinearLayout
                           audio = songs.getJSONObject(i).getString(TAG_AUDIO) ;
                           String chanson = songs.getJSONObject(i).getString("title") ;
                           String duration = songs.getJSONObject(i).getString(TAG_DURATION) ;

                           LinearLayout ll = new LinearLayout(getApplicationContext());
                           ll.setOrientation(LinearLayout.HORIZONTAL);

                           final TextView txtchanson = new TextView(getApplicationContext());
                           txtchanson.setText(chanson);
                           txtchanson.setPadding(10, 0, 10, 0);
                           TextView txtduration = new TextView(getApplicationContext());
                           txtduration.setText(duration);

                           final Button btn = new Button(getApplicationContext());
                           final MediaPlayer mp = new MediaPlayer(); 
                           mp.setAudioSessionId(i+1);

                           mp.setDataSource(audio); 
                           mp.prepare();

                           btn.setId(i+1);
                           btn.setTypeface(ionicons);
                           btn.setText(getString(R.string.play_str));
                           btn.setLayoutParams(params);

                           final int index = i;
                           mp.setOnCompletionListener(new OnCompletionListener() 
                             {

                                 public void onCompletion(MediaPlayer mp) 
                                 {

                                     btn.setText(getString(R.string.play_str));
                                 }
                             });

                             btn.setOnClickListener(new View.OnClickListener() {
                                    public void onClick(View v) 
                                    {


                                        Log.i("TAG", "index :" + index);
                                        Toast.makeText(getApplicationContext(), 
                                                "Clicked Button Index :" +  btn.getId(), 
                                                Toast.LENGTH_LONG).show();
                                        boolean b = false ;
                                        if ( b == false )

                                        {

                                            //txtchanson.setTextColor(R.color.green);
                                            mp.start();
                                            btn.setText(getString(R.string.pause_str));
                                            b = true;

                                        }

                                        else //if (btn.getText().equals( getString(R.string.pause_str)))
                                        {
                                            mp.pause();
                                            btn.setText(getString(R.string.play_str));
                                            //txtchanson.setTextColor(R.color.gray);
                                            b = false;

                                        }
                                    /*  else if (b == true )
                                        {
                                            btn.setText(getString(R.string.play_str));
                                            mp.pause();
                                        }*/
                                    }
                                });


                             ll.addView(btn);
                             ll.addView(txtchanson);
                             ll.addView(txtduration);

                             lm.addView(ll);  

                        }

你應該把你的boolean b = false ; 在onClick方法中排成一行,它應該作為全局變量,因為每次單擊時,當您轉到onClick方法時,您的b變量都會得到一個假值,並且永遠不會進入if-else語句的else子句。 一切都還好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM