java Android Mediaplayer 接连播放不同的歌曲
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12406110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Mediaplayer play different songs after eachother
提问by dorien
I generate midi files on the go. I want to play these files continuously.
我随时随地生成midi文件。我想连续播放这些文件。
I initialise a mediaplayer and start song1.mid. Then I use the following code to play song2.mid
我初始化一个媒体播放器并启动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();
}
}
});
}
});
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.
问题是媒体播放器在歌曲 2 之后停止。但我想要 Song3 开始。我可以增加全局变量,好的。但是当第二首歌结束时 onCompletionListener 似乎不起作用。
I guess I should initialise MediaPlayer mp to have an onCompletionListener too? Not sure what the best approach is.
我想我应该初始化 MediaPlayer mp 也有一个 onCompletionListener 吗?不确定最好的方法是什么。
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.
基本上,我想做的是连续播放song1.mid,song2.mid,...,但是文件是在程序中生成的。
EDITThanks to the wonderful help of @Gan. I know have the following working code:
编辑感谢@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();
}
}
});
}
});
回答by Gan
store the source locations in an array and in the onCompletionListener cycle through the source. something like this (use the same media player instance)
将源位置存储在一个数组中,并通过源存储在 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();
}
}