如何使“播放”和“暂停”图像按钮看起来像单个图像按钮.我现在附上我的代码.这些是使用的图像.我将游戏重命名为开始.
import androID.app.Activity;import androID.media.MediaPlayer;import androID.net.Uri;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Progressbar;public class AudioVIEw extends Activity{ MediaPlayer mp; button p,pu,s,b,f,h,v,c,bu; Progressbar myProgressbar; Uri uri=Uri.parse("http://player.trackitdown.net/prevIEw/289245/prevIEw_c-90-feat-red-monkey-yo-dj-original-mix-dos-or-dIE-traxx.mp3"); @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.audio_main); Log.v("Start Of OnCreate","++"); mp=MediaPlayer.create(this,uri); final button play = (button) findVIEwByID(R.ID.play); play.setBackgroundResource(R.drawable.start); final button pause = (button) findVIEwByID(R.ID.pause); pause.setBackgroundResource(R.drawable.pause); final button back = (button) findVIEwByID(R.ID.back); back.setBackgroundResource(R.drawable.backward); final button fwd = (button) findVIEwByID(R.ID.fwd); fwd.setBackgroundResource(R.drawable.forward); p=(button)findVIEwByID(R.ID.play); pu=(button)findVIEwByID(R.ID.pause); pu.setVisibility(VIEw.INVISIBLE); //s=(button)findVIEwByID(R.ID.stop); b=(button)findVIEwByID(R.ID.back); f=(button)findVIEwByID(R.ID.fwd); Log.v("button Objects","getting man"); play.setonClickListener(mListener); pause.setonClickListener(mListener); back.setonClickListener(mListener); fwd.setonClickListener(mListener); myProgressbar=(Progressbar)findVIEwByID(R.ID.progressbar_Horizontal); myProgressbar.setProgress(0); myProgressbar.setMax(mp.getDuration()); Log.v("End Of OnCreate","--"); } private Runnable myThread = new Runnable(){ public voID run() { // Todo auto-generated method stub while ( mp.getCurrentposition()<mp.getDuration()){ try{ //myHandle.sendMessage(myHandle.obtainMessage()); myProgressbar.setProgress( mp.getCurrentposition()); //Thread.sleep(1000); } catch(Throwable t){ } } } }; VIEw.OnClickListener mListener = new VIEw.OnClickListener(){ public voID onClick(VIEw v) { switch(v.getID()){ case R.ID.play: try { mp.prepare(); } catch (Exception e) { // Todo auto-generated catch block e.printstacktrace(); } mp.start(); p.setVisibility(VIEw.INVISIBLE); pu.setVisibility(VIEw.VISIBLE); pu.setClickable(true); p.setClickable(false); //seekbar.setProgress(mp.getCurrentposition()); new Thread(myThread).start(); break; case R.ID.pause: mp.pause(); pu.setClickable(false); p.setVisibility(VIEw.VISIBLE); pu.setVisibility(VIEw.INVISIBLE); p.setClickable(true); break; case R.ID.back: int dur = mp.getCurrentposition(); int pos = (dur>10000 ? dur-5000:0); mp.seekTo(pos); break; case R.ID.fwd: int curpos = mp.getCurrentposition(); int dur2 = mp.getDuration(); int pos2 = (curpos+5000>dur2 ? dur2: curpos+5000); mp.seekTo(pos2); break; } } }; }
而XML则是
<?xml version="1.0" enCoding="UTF-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" ><linearLayout androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_gravity="bottom" androID:gravity="center" > <ImageVIEw androID:layout_wIDth="176dp" androID:layout_height="208dp" androID:src="@drawable/audio_icon"/> <linearLayout androID:orIEntation="horizontal" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:gravity="center"> <button androID:ID="@+ID/play" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /> <button androID:ID="@+ID/pause" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="4dp" /> <button androID:ID="@+ID/back" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="4dp" /> <button androID:ID="@+ID/fwd" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="4dp" /> </linearLayout> <Progressbar androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:ID="@+ID/progressbar_Horizontal" /></linearLayout></linearLayout>
解决方法:
将按钮更改为首先播放.将播放和暂停代码合并为一个.设置并检查标志以查看是否按下播放或暂停并相应地更改文本.
所以你只有一个按钮和两个布尔字段:
boolean play=true, pause=false; final button playPause = (button) findVIEwByID(R.ID.play); playPause.setBackgroundResource(R.drawable.start); playPause.setonClickListener(mListener);
现在,在您的侦听器代码中,执行以下 *** 作:
case R.ID.play: if(play) { play=false; pause=true; //change image for button playPause.setBackgroundResource(R.drawable.start); try { mp.prepare(); } catch (Exception e) { // Todo auto-generated catch block e.printstacktrace(); } mp.start(); p.setVisibility(VIEw.INVISIBLE); pu.setVisibility(VIEw.VISIBLE); pu.setClickable(true); p.setClickable(false); //seekbar.setProgress(mp.getCurrentposition()); new Thread(myThread).start(); } if(pause) { play=true; pause=false; //change image for button playPause.setBackgroundResource(R.drawable.pause); mp.pause(); pu.setClickable(false); p.setVisibility(VIEw.VISIBLE); pu.setVisibility(VIEw.INVISIBLE); p.setClickable(true); } break;
相应地更改按钮上的文本.
总结以上是内存溢出为你收集整理的媒体播放器在Android中播放暂停全部内容,希望文章能够帮你解决媒体播放器在Android中播放暂停所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)