簡體   English   中英

Android Mediaplayer彼此播放不同的歌曲

[英]Android Mediaplayer play different songs after eachother

我可以隨時隨地生成Midi文件。 我想連續播放這些文件。

我初始化一個媒體播放器,然后啟動song1.mid。 然后,我使用以下代碼播放song2.mid

// set on completion listener music file
                mediaPlayer
                        .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {

                                String filePath2 = null;
                                File file = null;
                                FileInputStream inputStream = null;

                                //set the filePath
                                try {
                                    filePath2 = getCacheDir() + "/optimuse" + song + ".mid";
                                    file = new File(filePath2);
                                    if (file.exists()) {
                                        inputStream = new FileInputStream(file);
                                        if (inputStream.getFD().valid()) {
                                            System.out.println("Valid!");
                                        }
                                    }
                                } catch (Exception e1) {
                                    e1.printStackTrace();
                                    System.exit(-1);
                                }

                                //set Mediaplayer's datasource
                                if (file.exists()) {
                                    try {
                                        mediaPlayer = new MediaPlayer();
                                        mediaPlayer.setDataSource(inputStream.getFD());
                                        inputStream.close();
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                        System.exit(-1);
                                    }

                                    try {
                                        mediaPlayer.prepare();
                                    } catch (IllegalStateException e) {

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

                                //if the player is not running
                                if (!mediaPlayer.isPlaying()) {
                                    //start the player
                                    mediaPlayer.start();
                                    Toast.makeText(MainActivity.this,
                                            "mediaPlayer.start()", Toast.LENGTH_LONG)
                                            .show();
                                }
                            }
                        });                         


                            }
                        });

問題是媒體播放器在song2之后停止。 但我想開始Song3。 我可以增加全局變量,確定。 但是當第二首歌曲結束時,onCompletionListener似乎不起作用。

我想我也應該初始化MediaPlayer mp也要有onCompletionListener嗎? 不確定最佳方法是什么。

或者我應該做類似的事情:

new class MediaPlayer implements OnCompletionListener(){
@Override
song++;
//code to start the mediaplayer
}

謝謝您讓我朝正確的方向前進。 當我繼續啟動新的媒體播放器時,我也有點擔心效率。

基本上,我想做的是連續播放song1.mid,song2.mid,...,但是文件是在程序運行期間生成的。

編輯感謝@Gan的出色幫助。 我知道有以下工作代碼:

            // set on completion listener music file
            mediaPlayer
                    .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {

                            String filePath2 = null;
                            File file = null;
                            FileInputStream inputStream = null;

                            //set the filePath
                            try {
                                filePath2 = getCacheDir() + "/optimuse" + song + ".mid";
                                file = new File(filePath2);
                                if (file.exists()) {
                                    inputStream = new FileInputStream(file);
                                    if (inputStream.getFD().valid()) {
                                        System.out.println("Valid!");
                                    }
                                }
                            } catch (Exception e1) {
                                e1.printStackTrace();
                                System.exit(-1);
                            }

                            //set Mediaplayer's datasource
                            if (file.exists()) {
                                try {

                                    mp.stop();
                                    mp.reset();
                                    mp.setDataSource(inputStream.getFD());
                                    inputStream.close();
                                } catch (Exception e1) {
                                    e1.printStackTrace();
                                    System.exit(-1);
                                }

                                try {
                                    mp.prepare();
                                } catch (IllegalStateException e) {

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

                            //if the player is not running
                            if (!mp.isPlaying()) {
                                //start the player
                                mp.start();
                                Toast.makeText(MainActivity.this,
                                        "mp.start()", Toast.LENGTH_LONG)
                                        .show();
                            }


                        }
                    });                         


                        }
                    });

將源位置存儲在數組中,並在整個源的onCompletionListener循環中存儲。 這樣的事情(使用相同的媒體播放器實例)

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) 
{
   if(currentPosition<sourceArray.size())
   {
        mediaPlayer.reset();
       /* load the new source */
       mediaPlayer.setDataSource(sourceArray[position]);
       /* Prepare the mediaplayer */
       mediaPlayer.prepare();
       /* start */
       mediaPlayer.start();
   }
   else
   {
       /* release mediaplayer */
       mediaPlayer.release();
   }
 }

暫無
暫無

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

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