我在ActionBar中创建了一个MediaPlayer按钮,当应用程序启动时它会自动播放,当我点击它时它应该关闭。现在的问题是声音没有播放,当我点击按钮时,logcat中有一个错误说...
更新的LOGCAT。
12-18 19:04:57.812: E/MediaPlayer(5673): attachNewPlayer called in state 32
这是代码..
public class MainActivity extends Activity{
private MediaPlayer mp;
Item btnplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = new MediaPlayer();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
isPlaying=false;//when the media file playing is completed isPlaying=false; is must
}
});
play(null);//you are calling play by launching
}
//inflate items in actionbar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
boolean isPlaying = false;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
if (item.getItemId() == R.id.btnplay) //whatever you named in xml
{
play(item);
}
return super.onOptionsItemSelected(item);
}
public void play(MenuItem menuItem) {
if (!isPlaying) {
try
{
AssetFileDescriptor afd = getAssets().openFd("caketown.mp3");
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();//play sound
}
catch(Exception e) {
e.printStackTrace();
}
isPlaying = true;
}
else if (isPlaying) {
mp.pause();
isPlaying = false;
}
}
这是菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.xxx.MainActivity" >
<item
android:id="@+id/btnplay"
android:title=""
android:icon="@drawable/ic_action_volume_on"
android:onClick="play"
android:showAsAction="always"/>
<menu>
请帮帮我。我刚来这里。 :(
答案 0 :(得分:0)
1.请在mp = new MediaPlayer();
方法
onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play(null);//you are calling play by launching
}
2.将其更改为boolean isPlaying = false;
,因为当应用启动时,它会设置为boolean isPlaying = false
,因此它会播放音频并再次点击它暂停,它将按照您的代码运行
3.更改此else if
else if (isPlaying) {
mp.pause();
isPlaying=false;
}
4.也改变这个功能
public void play(MenuItem menuItem) {
if (!isPlaying && !isPaused) {
try {
if (mp == null) {
// mp.release();
// mp.reset();
mp = new MediaPlayer();
AssetFileDescriptor afd = getAssets()
.openFd("caketown.mp3");
mp.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
isPlaying = false;// when the media file playing is completed
isPaused = false;
// Toast.makeText(getApplicationContext(), "play completed",
// Toast.LENGTH_SHORT).show(); // isPlaying=false; is must
}
});
}
mp.start();// play sound
} catch (Exception e) {
e.printStackTrace();
}
isPlaying = true;
} else if (isPlaying) {
Toast.makeText(getApplicationContext(), "play paused",
Toast.LENGTH_SHORT).show();
mp.pause();
isPlaying = false;
isPaused = true;
length = mp.getCurrentPosition();
// and for resuming the player from the position where it stopped
// lately is done by:
} else if (isPaused) {
mp.seekTo(length);
mp.start();
isPlaying = true;
isPaused = false;
}
}
答案 1 :(得分:0)
将isPlaying
初始化为false以使您的条件有效。
仅初始化MediaPlayer
一次以使停止/暂停工作而不是重叠声音。也就是说,例如将mp = new MediaPlayer();
移至onCreate()
。