问题描述
所以,这是东西.我遵循《新波士顿教程》为活动添加背景音乐.但是后来我想,如果我可以添加歌曲列表,然后使用随机数在后台播放列表中的歌曲,那会不是很好.现在,它可以工作了,但事情是这样,在我按了两次播放按钮后,它给了我这个错误消息(下面的图像以红色显示),并且我无法理解该错误消息的含义.谁能建议我如何解决错误?
So, here is the thing. I was following the New Boston Tutorials for adding background music to an activity. But then i was thinking wouldn't it be nice if i can add a list of songs and then use the Random number to play songs from the list in the background. Now, it works but here is the thing, after i press the play button twice it gives me this error message(image attached below in red) and i am unable to understand what the error message means. Can anyone suggest me how i can fix the error?
package com.example.user.testapp5;
import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
import static com.example.user.testapp5.R.raw.u;
public class MainActivity extends Activity {
Button b1,b2;
TextView t1;
MediaPlayer [] s = new MediaPlayer[2];
int n;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s[0] = MediaPlayer.create(MainActivity.this,R.raw.u);
s[1] = MediaPlayer.create(MainActivity.this,R.raw.k);
b1 = (Button)findViewById(R.id.display);//start button to play the song
b2 = (Button)findViewById(R.id.Sbutton);//stop button to stop the song
t1 = (TextView)findViewById(R.id.Tdisplay);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
t1.setText("Hey Welcome");
t1.setTextColor(Color.BLUE);
t1.setTextSize(10);
for(int i =0 ;i<1;i++)
{
Random r = new Random();
n = r.nextInt(2);
s[n].start();
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
s[n].stop();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
推荐答案
这是因为媒体播放器尚未准备好播放您的歌曲.在为Media Player实例设置数据源之后,您应该立即调用 player.prepareAsync();
,当准备好Media Player之后,将执行 onPrepared
方法.方法,开始播放:
This is because media player is not ready to play your song yet. After setting data source for the Media Player instance, you should call player.prepareAsync();
now when the Media Player is prepared, the onPrepared
method will be executed.Inside this method, start the playback:
@Override
public void onPrepared(MediaPlayer mp) {
//start playback
mp.start();
}
完成任务后,您应该释放所有资源,例如:
And after completion of your task you should release all resources like :
player.stop();
player.release();
为避免同一首媒体播放器实例同时播放多首歌曲,我建议创建单例类,该类一次仅返回一个实例.如果您使用服务来播放背景歌曲,我建议您遵循本教程:- http://code.tutsplus.com/tutorials/create-a-music-player-on-android-song-playback--mobile-22778
希望这对您有帮助!!!
For avoiding multiple songs play simultaneously with same media player instance I would suggest to create singleton class which returns only one instance at a time.If you are using service for background song playing, I would suggest to follow this tutorial :- http://code.tutsplus.com/tutorials/create-a-music-player-on-android-song-playback--mobile-22778
Hope this will help you !!!
这篇关于使用androidStudio中的MediaPlayer播放列表中的随机歌曲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!