Android的媒体播放器后海誓山盟播放不同的歌曲 [英] Android Mediaplayer play different songs after eachother
问题描述
我产生在旅途MIDI文件。我想连续播放这些文件。
我初始化一个媒体播放器,并开始song1.mid。然后我用下面的code打song2.mid
//在完成监听音乐文件设置
媒体播放器
.setOnCompletionListener(新MediaPlayer.OnCompletionListener(){
@覆盖
公共无效onCompletion(MediaPlayer的MP){ 字符串FILEPATH2 = NULL;
文件file = NULL;
的FileInputStream的InputStream = NULL; //设置文件路径
尝试{
FILEPATH2 = getCacheDir()+/ optimuse+歌+.MID
文件=新的文件(FILEPATH2);
如果(file.exists()){
的InputStream =新的FileInputStream(文件);
如果(inputStream.getFD()。有效()){
的System.out.println(有效!);
}
}
}赶上(例外E1){
e1.printStackTrace();
System.exit(-1);
} //设置MediaPlayer的数据源
如果(file.exists()){
尝试{
媒体播放器=新的MediaPlayer();
mediaPlayer.setDataSource(inputStream.getFD());
inputStream.close();
}赶上(例外E1){
e1.printStackTrace();
System.exit(-1);
} 尝试{
媒体播放器prepare()。
}赶上(IllegalStateException异常五){ e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
} //如果玩家没有运行
如果(!mediaPlayer.isPlaying()){
//启动播放器
mediaPlayer.start();
Toast.makeText(MainActivity.this,
mediaPlayer.start(),Toast.LENGTH_LONG)
。显示();
}
}
});
}
});
的问题是,在媒体播放器song2后停止。但我想song3启动。我可以增加全局变量,确定。但onCompletionListener似乎并不当第二首歌曲结束工作。
我想我应该初始化MediaPlayer的熔点有一个onCompletionListener呢?不知道最好的方法是什么。
或者我应该这样做:
类新的MediaPlayer实现OnCompletionListener(){
@覆盖
歌++;
// code键启动媒体播放器
}
感谢您把我在正确的方向。我也关注效率一点点,当我不断启动新的mediaplayers ...
基本上,我想要做的是发挥song1.mid,song2.mid,......不断,但文件在程序过程中产生的。
修改
由于@Gan美妙的帮助。我知道有以下工作code:
//在完成监听音乐文件设置
媒体播放器
.setOnCompletionListener(新MediaPlayer.OnCompletionListener(){
@覆盖
公共无效onCompletion(MediaPlayer的MP){ 字符串FILEPATH2 = NULL;
文件file = NULL;
的FileInputStream的InputStream = NULL; //设置文件路径
尝试{
FILEPATH2 = getCacheDir()+/ optimuse+歌+.MID
文件=新的文件(FILEPATH2);
如果(file.exists()){
的InputStream =新的FileInputStream(文件);
如果(inputStream.getFD()。有效()){
的System.out.println(有效!);
}
}
}赶上(例外E1){
e1.printStackTrace();
System.exit(-1);
} //设置MediaPlayer的数据源
如果(file.exists()){
尝试{ mp.stop();
mp.reset();
mp.setDataSource(inputStream.getFD());
inputStream.close();
}赶上(例外E1){
e1.printStackTrace();
System.exit(-1);
} 尝试{
MP prepare()。
}赶上(IllegalStateException异常五){ e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
} //如果玩家没有运行
如果(!mp.isPlaying()){
//启动播放器
mp.start();
Toast.makeText(MainActivity.this,
mp.start(),Toast.LENGTH_LONG)
。显示();
}
}
});
}
});
存储在数组中,并通过源头onCompletionListener周期的源位置。这样的事情(使用相同的媒体播放器实例)
mediaPlayer.setOnCompletionListener(新MediaPlayer.OnCompletionListener(){
@覆盖
公共无效onCompletion(MediaPlayer的MP)
{
如果(currentPosition&下; sourceArray.size())
{
mediaPlayer.reset();
/ *加载新源* /
mediaPlayer.setDataSource(sourceArray [位置]);
/ * prepare的媒体播放器* /
媒体播放器prepare()。
/ * *启动/
mediaPlayer.start();
}
其他
{
/ *版本媒体播放器* /
mediaPlayer.release();
}
}
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();
}
}
});
}
});
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();
}
}
这篇关于Android的媒体播放器后海誓山盟播放不同的歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!