在Android的媒体播放器播放暂停 [英] media player play pause in android

查看:114
本文介绍了在Android的媒体播放器播放暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能让播放和暂停图像按钮看起来像一个单一的图像按钮。在此我附上我下面的code。这些是使用的图像。我改名剧中饰演的开始。

 进口android.app.Activity;
进口android.media.MediaPlayer;
进口android.net.Uri;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.Button;
进口android.widget.ProgressBar;


公共类AudioView延伸活动{

 MediaPlayer的熔点;

    按钮p,PU,S,B,F,H,V,C,卜;

     进度myProgressBar;

        乌里uri=Uri.parse("http://player.trackitdown.net/$p$pview/289245/$p$pview_c-90-feat-red-monkey-yo-dj-original-mix-dos-or-die-traxx.mp3");

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.audio_main);

        Log.v(启动的OnCreate,++);
        MP = MediaPlayer.create(这一点,URI);


        最终按钮播放=(按钮)findViewById(R.id.play);
        play.setBackgroundResource(R.drawable.start);
        最终按钮暂停=(按钮)findViewById(R.id.pause);
        pause.setBackgroundResource(R.drawable.pause);
        最终按钮回到=(按钮)findViewById(R.id.back);
        back.setBackgroundResource(R.drawable.backward);
        最终按钮FWD =(按钮)findViewById(R.id.fwd);
        fwd.setBackgroundResource(R.drawable.forward);

        P =(按钮)findViewById(R.id.play);
        PU =(按钮)findViewById(R.id.pause);
        pu.setVisibility(View.INVISIBLE);
        //s=(Button)findViewById(R.id.stop);
        B =(按钮)findViewById(R.id.back);
        F =(按钮)findViewById(R.id.fwd);

        Log.v(键对象,让男人);

        play.setOnClickListener(mListener);
        pause.setOnClickListener(mListener);
        back.setOnClickListener(mListener);
        fwd.setOnClickListener(mListener);


        myProgressBar =(进度)findViewById(R.id.progressbar_Horizo​​ntal);

        myProgressBar.setProgress(0);
        myProgressBar.setMax(mp.getDuration());


        Log.v(结束的OnCreate, - );




    }
    私人可运行myThread =新的Runnable(){

          公共无效的run(){
           // TODO自动生成方法存根
           而(mp.getCurrentPosition()&其中; mp.getDuration()){
            尝试{
             //myHandle.sendMessage(myHandle.obtainMessage());
                  myProgressBar.setProgress(mp.getCurrentPosition());
                //Thread.sleep(1000);
            }
            捕获(的Throwable T){
            }
           }
          }

            };


    View.OnClickListener mListener =新View.OnClickListener(){

        公共无效的onClick(视图v){

            开关(v.getId()){

            案例R.id.play:

                尝试 {
                    MP prepare()。
                }赶上(例外五){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }


                mp.start();
                p.setVisibility(View.INVISIBLE);
                pu.setVisibility(View.VISIBLE);
                pu.setClickable(真正的);
                p.setClickable(假);
                   //seekBar.setProgress(mp.getCurrentPosition());
                   新的Thread(myThread)。开始();

                打破;
            案例R.id.pause:
                mp.pause();
                pu.setClickable(假);
                p.setVisibility(View.VISIBLE);
                pu.setVisibility(View.INVISIBLE);
                p.setClickable(真正的);
                打破;

            案例R.id.back:
                INT DUR = mp.getCurrentPosition();
                INT POS =(杜尔> 10000 DUR-5000:0);
                mp.seekTo(POS);
                打破;
            案例R.id.fwd:
                INT CURPOS = mp.getCurrentPosition();
                INT dur2 = mp.getDuration();

                INT POS2 =(CURPOS + 5000> dur2 dur2:CURPOS + 5000);
                mp.seekTo(POS2);
                打破;


            }

        }


    };

   }
 

而XML是

 < XML版本=1.0编码=UTF-8&GT?;

< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android

    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >

<的LinearLayout机器人:方向=垂直

    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =底
    机器人:重力=中心
    >

    < ImageView的机器人:layout_width =176dp
               机器人:layout_height =208dp
               机器人:SRC =@可绘制/ audio_icon/>

       <的LinearLayout
        机器人:方向=横向
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=中心>

        <按钮
        机器人:ID =@ + ID /播放
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        />

        <按钮
            机器人:ID =@ + ID /暂停
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginLeft =4DP/>

        <按钮
        机器人:ID =@ + ID /后退
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =4DP
        />
        <按钮
        机器人:ID =@ + ID / FWD
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =4DP
        />

    < / LinearLayout中>

    <进度
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    风格=机器人:ATTR / progressBarStyleHorizo​​ntal
    机器人:ID =@ + ID / progressbar_Horizo​​ntal
    />

< / LinearLayout中>
< / LinearLayout中>
 

解决方案

更​​改按钮,说玩在第一。结合播放和暂停code为一体。设置和检查一个标志来判断是否播放或暂停为pressed并相应修改文本。

所以你只有一个按键,两个布尔字​​段:

 布尔播放= TRUE,暂停= FALSE;

    最终按钮playPause =(按钮)findViewById(R.id.play);
            playPause.setBackgroundResource(R.drawable.start);

    playPause.setOnClickListener(mListener);
 

现在,在你的听众code,做到这一点:

 案例R.id.play:

            如果(播放)
            {
                玩= FALSE;
                暂停= TRUE;

                //改变图像按钮
                playPause.setBackgroundResource(R.drawable.start);

                尝试 {
                    MP prepare()。
                }赶上(例外五){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }


                mp.start();
                p.setVisibility(View.INVISIBLE);
                pu.setVisibility(View.VISIBLE);
                pu.setClickable(真正的);
                p.setClickable(假);
                   //seekBar.setProgress(mp.getCurrentPosition());
                   新的Thread(myThread)。开始();

                }
            如果(暂停)
            {
                播放= TRUE;
                暂停= FALSE;

                 //改变图像按钮
                playPause.setBackgroundResource(R.drawable.pause);

                mp.pause();
                pu.setClickable(假);
                p.setVisibility(View.VISIBLE);
                pu.setVisibility(View.INVISIBLE);
                p.setClickable(真正的);
            }
            打破;
 

在您的按钮相应地更改文本。

How can I make the Play and Pause image buttons look as a single image button. I am hereby attaching my code below. These are the images used. i Renamed play as start.

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;


            }

        }


    };

   }

And the XML is

<?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" 
    style="?android:attr/progressBarStyleHorizontal"
    android:id="@+id/progressbar_Horizontal"
    />

</LinearLayout>
</LinearLayout>

解决方案

Change your button to say Play at first. Combine play and pause code into one. Set and check a flag to see if play or pause is pressed and change text accordingly.

So you'll only have one button and two boolean fields:

 boolean play=true, pause=false;

    final Button playPause = (Button) findViewById(R.id.play);
            playPause.setBackgroundResource(R.drawable.start);

    playPause.setOnClickListener(mListener);

Now, in your listener code, do this:

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;

Change the text on your button accordingly.

这篇关于在Android的媒体播放器播放暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆