Android Mediaplayer play different songs after eachother

点点圈 提交于 2019-12-23 01:46:37

问题


I generate midi files on the go. I want to play these files continuously.

I initialise a mediaplayer and start song1.mid. Then I use the following code to play 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();
                                }
                            }
                        });                         


                            }
                        });

The problem is that the mediaplayer stops after song2. But I want song3 to start. I can increment the global variable, ok. But the onCompletionListener does not seem to work when the second song finishes.

I guess I should initialise MediaPlayer mp to have an onCompletionListener too? Not sure what the best approach is.

Or should I do something like:

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

Thank you for putting me in the right direction. I am also a little bit concerned with efficiency, when I keep starting up new mediaplayers...

Basically, what I want to do is play song1.mid, song2.mid,... continuously, but the files are generated during the program.

EDIT Thanks to the wonderful help of @Gan. I know have the following working code:

            // 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();
                            }


                        }
                    });                         


                        }
                    });

回答1:


store the source locations in an array and in the onCompletionListener cycle through the source. something like this (use the same media player instance)

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();
   }
 }


来源:https://stackoverflow.com/questions/12406110/android-mediaplayer-play-different-songs-after-eachother

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!