如何将歌曲的状态从停止更改为开始?
我在android中制作了一个应用程序,其中我在xml中采用了三个按钮,在java中我设置了onclick事件,
当单击播放按钮时,歌曲已准备好并播放, 单击停止按钮时歌曲停止, 单击暂停按钮时,歌曲暂停。
但我的问题是,当我单击播放按钮时,在单击停止按钮后播放歌曲,然后在单击播放按钮后停止播放歌曲,但此时未播放歌曲,这是
我为此应用程序创建的代码。下面给出。
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(position > 0)
{
mp.seekTo(position);
mp.start();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
请参阅此更新的代码:
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp.isPlaying() == false)
{
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
I have make one application in android in which i take three buttons in xml and in java i set the onclick event,
when play button is clicked song is prepared and play,
when stop button is clicked song is stopped,
when pause button is clicked song is paused.
But my problem is that when I clicked play button song is played after i click on stop button then song is stopped after I clicked on play button, but at this time song is not played,
the code I created for this app. is given below.
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(position > 0)
{
mp.seekTo(position);
mp.start();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
see this Updated code:
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp.isPlaying() == false)
{
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑::
您需要在播放事件中应用条件,例如 ::
Edit ::
you need to apply condition in play event like ::