繁体   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