简体   繁体   English

一个接一个地播放音乐

[英]Play music one after each other

I am working on a game in Android and i wanted to add a sort of playlist of music. 我正在开发Android游戏,我想添加一种音乐播放列表。 I have about 10 music files and i want to be able to play the second one when the first one has finished and so on. 我有大约10个音乐文件,我希望能够在第一个完成后播放第二个音乐文件,依此类推。 Is there anyway to do this? 反正有没有这样做?

Use the Android Mediaplayer and use an OnCompletionListener in which you send a message to a handler, from which you pass the next audiofile to the mediaplayer. 使用Android Mediaplayer并使用OnCompletionListener,您可以在其中向处理程序发送消息,从中将下一个音频文件传递给mediaplayer。

EDIT : 编辑:

You do not really need to use a handler. 你真的不需要使用处理程序。 Here is an example of how you could implement it. 以下是如何实现它的示例。
Save your titles and a counter as member variables: 将您的标题和计数器保存为成员变量:

String[] titles = new String[] { "title1.mp3", "title2.mp3", "title3.mp3",
        "title4.mp3" };
int count = 0;

And this is how your onCompletion listener would look like: 这就是你的onCompletion监听器的样子:

    void onCompletion(MediaPlayer mp){
           mp.stop();
           if (count == titles.length -1) {
               count = 0;
           }
           mp.setDataSource(titles[count]);
           count++;
           mp.prepare(); 
           mp.start();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM