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 中的媒体播放器播放暂停 的相关文章

  • CFdump cfcomponent cfscript

    可以在 cfcomponent 中使用 cfdump 吗 可以在 cfscript 中使用 cfdump 吗 我知道 anser 不是 那么如何发出 insde cfcomponent 函数的值 cf脚本 我用的是CF8 可以在 cfcom
  • 如何确定所有角度2分量都已渲染?

    当所有 Angular2 组件完成渲染时 是否会触发一个角度事件 For jQuery 我们可以用 function 然而 对于 Angular2 当domready事件被触发 html 只包含角度组件标签 每个组件完成渲染后 domrea
  • TIFF 元数据的最大大小是多少?

    TIFF 文件元数据的单个字段中可以合并的元数据数量是否有最大限制 我想在 ImageDescription 字段中存储大文本 最多几 MB 没有具体的最大限制ImageDescription但是 整个 TIFF 文件存在最大文件大小 该最
  • 如何在执行新操作时取消先前操作的执行?

    我有一个动作创建器 它会进行昂贵的计算 并在每次用户输入内容时调度一个动作 基本上是实时更新 但是 如果用户输入多个内容 我不希望之前昂贵的计算完全运行 理想情况下 我希望能够取消执行先前的计算并只执行当前的计算 没有内置功能可以取消Pro
  • 使用.NET技术录制屏幕视频[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有没有一种方法可以使用 NET 技术来录制屏幕 无论是桌面还是窗口 我的目标是免费的 我喜欢小型 低
  • Redis如何存储关联数组?设置、散列还是列表?

    我对 Redis 的所有可用存储选项有点困惑 我想做一些简单的事情 并且不想过度设计它 我正在与phpredis and Redis v2 8 6 我有一个需要存储的简单关联数组 我还需要能够通过其键检索项目并循环遍历所有项目 a arra
  • PHPUnit 和 Zend Framework assertRedirectTo() 问题

    我在创建的测试中遇到了 assertRedirectTo 问题 下面是我使用的代码 public function testLoggedInIndexAction this gt dispatch this gt assertControl
  • 从超立方体图像中获取文本的确切位置

    使用 tesseract 中的 GetHOCRText 0 方法 我能够检索 html 中的文本 并在 webview 中呈现 html 时 我能够获取文本 但图像中文本的位置与输出不同 任何想法都非常有帮助 tesseract gt Se
  • 节拍匹配算法

    我最近开始尝试创建一个移动应用程序 iOS Android 它将自动击败比赛 http en wikipedia org wiki Beatmatching http en wikipedia org wiki Beatmatching 两
  • Spring Boot @ConfigurationProperties 不从环境中检索属性

    我正在使用 Spring Boot 1 2 1 并尝试创建一个 ConfigurationProperties带有验证的bean 如下所示 package com sampleapp import java net URL import j
  • 对来自流读取器的过滤数据执行小计

    编辑问题未得到解答 我有一个基于 1 个标准的过滤输出 前 3 个数字是 110 210 或 310 给出 3 个不同的组 从流阅读器控制台 问题已编辑 因为第一个答案是我给出的具体示例的字面解决方案 我使用的实际字符串长度为 450 个
  • 循环内的异步性

    我正在使用 jQuery getJSON 用于从一组实用程序的给定 URL 检索数据的 API 我真的很想找到一种为每个实用程序重用代码 完全相同 的方法 由于循环的执行与 ajax 调用无关 因此我无法找到保留循环值的方法 我知道这个描述
  • neo4j - python 驱动程序,服务不可用

    我对 neo4j 非常陌生 我正在尝试建立从 python3 6 到 neo4j 的连接 我已经安装了驱动程序 并且刚刚开始执行第一步 导入请求 导入操作系统 导入时间 导入urllib 从 neo4j v1 导入 GraphDatabas
  • rspec 中的模拟方法链

    有一系列方法可以获得user目的 我试图模拟以下内容以返回user in my Factory Girl current user AuthorizeApiRequest call request headers result 我可以模拟该
  • 使用 xpath 和 vtd-xml 以字符串形式获取元素的子节点和文本

    这是我的 XML 的一部分
  • 如何使用 Pycharm 安装 tkinter? [复制]

    这个问题在这里已经有答案了 I used sudo apt get install python3 6 tk而且效果很好 如果我在终端中打开 python Tkinter 就可以工作 但我无法将其安装在我的 Pycharm 项目上 pip
  • Statsmodels.formula.api OLS不显示截距的统计值

    我正在运行以下源代码 import statsmodels formula api as sm Add one column of ones for the intercept term X np append arr np ones 50
  • 在 Nexus 7 2013 上更改方向时 CSS 媒体查询不起作用

    我目前正在我的笔记本电脑 台式电脑和 Nexus 7 2013 上测试 CSS 媒体查询 除了 Nexus 7 之外 它们在台式机和笔记本电脑上都运行良好 当我更改方向时 除非刷新页面 否则样式不会应用 例如 以纵向模式握住设备时 页面正常
  • 如何在react-highcharts中使用图表工具提示格式化程序?

    如何使用图表工具提示格式化程序 我正在使用高图表的反应包装器 我有这样的配置 const CHART CONFIG tooltip formatter tooltip gt var s b this x b each this points
  • 强制 Listview 不重复使用视图(复选框)

    我做了一个定制Listview 没有覆盖getView 方法 Listview 中的每个项目都具有以下布局 联系布局 xml

随机推荐