Android 中的媒体播放器播放暂停

2023-12-11

如何使“播放”和“暂停”图像按钮看起来像单个图像按钮。我特此在下面附上我的代码。这些是使用的图像。我将播放重命名为开始。

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" 
    style="?android:attr/progressBarStyleHorizontal"
    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;

相应地更改按钮上的文本。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 中的媒体播放器播放暂停 的相关文章

  • ListItem 附加自定义值

    我在asp net中使用dropdownlist 它有代表下拉列表项目的ListItem集合 每个ListItem只有两个字段来保存数据 Value和Text字段 但这些还不够 我想保存更多数据对于每个项目 假设附加字段中有 Text1 和
  • java8 Collectors.toMap() 限制?

    我正在尝试使用java8Collectors toMap on a Stream of ZipEntry 这可能不是最好的想法 因为在处理过程中可能会发生异常 但我想这应该是可能的 我现在收到一个我不明白的编译错误 我猜是类型推理引擎 这是
  • Excel 2013 数据透视表不会更改当前页面,除非手动导航到

    我们有一小段 VBA 代码 多年来一直完美运行 本质上是 Me PivotTables APivot PivotFields AField CurrentPage Some text 这种方法一直有效 直到 Excel 2013 该行将失败
  • 将带有星号的注册表项传递给测试路径

    我想通过以下方式运行此注册表路径Test Path在 PowerShell 中 但它包含一个星号 该星号在注册表中有效 但在 Windows 路径中无效 问题是 当我通过它时 Test Path将星号视为通配符 因此这需要非常非常长的时间
  • 如何从 Magento One Page Checkout 获取发布数据?

    为了在 Magento Checkout 中添加客户评论字段 我在相应的模板文件中添加了一个文本字段 并使用如下观察器将评论添加到订单中 comment strip tags Mage app gt getRequest gt getPar
  • 多边形内的 SQL 地理点在 STIntersect 上不返回 true(但使用 Geometry 返回 true)

    我不想仅仅为了在 STIntersect 中返回 true 而将地理数据转换为几何图形 下面是 SQL 中的代码 DECLARE point GEOGRAPHY GEOGRAPHY Point 1 1 4326 DECLARE polygo
  • 如何将变量插入 PHP 数组?

    我在网上查了一些答案 但都不是很准确 我希望能够做到这一点 id result id info array id Example echo info 0 这有可能吗 您需要的是 不推荐 info array id Example varia
  • javax.persistence.Table.indexes()[Ljavax/persistence/Index 中的 NoSuchMethodError

    我有一个 Play Framework 应用程序 并且我was使用 Hibernate 4 2 5 Final 通过 Maven 依赖项管理器检索 我决定升级到 Hibernate 4 3 0 Final 成功重新编译我的应用程序并运行它
  • 如何更改 aptana studio 的背景颜色?

    如何将 Aptana IDE 或整个主题 的黑色背景更改为其他背景 例如蓝色 正如 gyozo 在评论中提到的 对于蓝色主题 请使用 窗口 gt 首选项 gt Aptana Studio gt 主题 并选择 Eclipse 主题
  • 用javascript调用外部网页(跨域)

    我正在尝试使用以下网络服务来验证提要这个问题 https stackoverflow com questions 11996430 check if a url is a valid feed 但浏览器不允许我向另一台服务器发送 ajax
  • 如何将十六进制字符串转换为无符号长整型?

    我有以下十六进制值 CString str str T FFF000 如何将其转换为unsigned long 您可以使用strtol作用于常规 C 字符串的函数 它使用指定的基数将字符串转换为 long long l strtol str
  • 在Python中停止ThreadPool中的进程

    我一直在尝试为控制某些硬件的库编写一个交互式包装器 用于 ipython 有些调用对 IO 的影响很大 因此并行执行任务是有意义的 使用 ThreadPool 几乎 效果很好 from multiprocessing pool import
  • Jackson 将单个项目反序列化到列表中

    我正在尝试使用一项服务 该服务为我提供了一个带有数组字段的实体 id 23233 items name item 1 name item 2 但是 当数组包含单个项目时 将返回该项目本身 而不是包含一个元素的数组 id 43567 item
  • 不区分大小写的字符串比较 C++ [重复]

    这个问题在这里已经有答案了 我知道有一些方法可以进行忽略大小写的比较 其中涉及遍历字符串或一个good one https stackoverflow com questions 11635 case insensitive string
  • Biopython 可以执行 Seq.find() 来解释歧义代码吗

    我希望能够在 Seq 对象中搜索考虑歧义代码的子序列 Seq 对象 例如 以下内容应该是正确的 from Bio Seq import Seq from Bio Alphabet IUPAC import IUPACAmbiguousDNA
  • 使用适用于 Android 和 ios 的 Angular NativeScript 的透明选项卡栏和操作栏

    我想让标签栏透明 操作栏在滑动布局或页面上透明 操作栏或选项卡栏必须位于页面顶部 就像两层一样 我尝试过使用 css 使其透明 但它在页面上并没有变得透明
  • JQuery 删除和内存泄漏

    我正在开发一个游戏 我看到了很多内存消耗 我使用jquery animate 动画完成后 我 remove 元素 我的问题是 从 dom 树中删除一个元素后 对象还存在记忆中吗 Javascript 是一种垃圾收集语言 这意味着当没有代码保
  • ubuntu:升级软件(cmake)-版本消歧(本地编译)[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我的机器上安装了 cmake 2 8 0 来自 ubuntu 软件包 二进制文件放置在 usr bin cmake 中 我需要将 cmake 版本至少
  • 在 Google 地图上绘制线条/路径

    我很长一段时间都在忙于寻找如何在 HelloMapView 中的地图上的两个 GPS 点之间画一条线 但没有运气 谁能告诉我该怎么做 假设我使用扩展 MapView 的 HelloMapView 我需要使用叠加层吗 如果是这样 我是否必须重
  • OpenCV SIFT 描述符关键点半径

    我正在深入研究OpenCV的SIFT描述符提取的实现 https github com Itseez opencv blob master modules nonfree src sift cpp 我发现了一些令人费解的代码来获取兴趣点邻域

随机推荐