[英]Android - MediaPlayer error: attachnewplayer called in state 64
[英]Mediaplayer : pause called in state 1 - Android Eclipse
我正在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>
請幫我。 我只是新來的。 :(
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;
}
}
Initialize isPlaying
為false以使您的條件正常工作。
僅初始化一次MediaPlayer
即可使停止/暫停工作,而不會使聲音重疊。 也就是說,移動mp = new MediaPlayer();
以onCreate()
為例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.