• 【Android】点击按钮播放音乐,再次点击停止播放


    1、在res下新建raw文件夹

    请添加图片描述
    将音频复制粘贴至文件夹
    请添加图片描述

    2、代码

    (1)PlatformActivity.java

    	public static int cnt = 0;
        SoundPool sp;//声明SoundPool的引用
        HashMap<Integer, Integer> hm;//声明HashMap来存放声音文件
        int currStaeamId;//当前正播放的streamId
    
    • 1
    • 2
    • 3
    • 4
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_platform);
    
            ImageButton btn_play = (ImageButton) this.findViewById(R.id.imagbutton_playlater);
            initSoundPool();//初始化声音池的方法
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
        public void playSound(int sound, int loop) {//获取AudioManager引用
            AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
            //获取当前音量
            float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
            //获取系统最大音量
            float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            //计算得到播放音量
            float volume = streamVolumeCurrent / streamVolumeMax;
            //调用SoundPool的play方法来播放声音文件
            currStaeamId = sp.play(hm.get(sound), volume, volume, 1, loop, 1.0f);
        }
    
        public void initSoundPool() {//初始化声音池
            sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);//创建SoundPool对象
            hm = new HashMap<Integer, Integer>();//创建HashMap对象
            //加载声音文件,并且设置为1号声音放入hm中
            hm.put(1, sp.load(this, R.raw.en01, 1));
        }
    
        public void onclickPlay(View view){
            //点击按钮播放音乐,再次点击停止播放
            if(cnt==0){
                cnt = 1;
            }
            else {
                cnt = 0;
            }
            if(cnt==1) {
                playSound(1, 0);//播放1号声音资源,且播放一次
                //提示播放即时音效
                Toast.makeText(PlatformActivity.this, "播放音效", Toast.LENGTH_SHORT).show();
            }
            else {
                sp.stop(currStaeamId);//停止正在播放的某个声音
                //提示停止播放
                Toast.makeText(PlatformActivity.this, "停止播放音效", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    (2)fragmentspeech.xml
    设置一个普通的按钮就好啦,我写的是一个imagebutton

    <ImageButton
                android:id="@+id/imagbutton_playlater2"
                android:layout_width="22dp"
                android:layout_height="27dp"
                android:layout_marginLeft="21dp"
                android:layout_marginTop="30dp"
                android:background="#00FF0000"
                android:onClick="onclickPlay"
                android:scaleType="centerInside"
                android:src="@drawable/icon_play_selected03" />
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    3、运行结果

    请添加图片描述
    点击播放后
    请添加图片描述
    再次点击按钮
    请添加图片描述

  • 相关阅读:
    任务调度之ScheduledThreadPoolExecutor源码分析
    java 使用documents4j将XML转为pdf文件的方式
    Spring_第2章_注解开发+整合Mybatis+Junit
    【机器学习算法】穿越神经网络的迷雾:深入探索机器学习的核心算法
    一个实用的链接导航页的站点设计 支持自定义链接
    CSGO社区服务器开服架设搭建教程windows服务器什么配置的合适国际服
    交通目标检测-行人车辆检测流量计数 - 计算机竞赛
    Cilium系列-4-Cilium本地路由
    高光谱解混和图片去噪(Matlab代码实现)
    matlab里BP神经网络实现实例2汽油辛烷值预测
  • 原文地址:https://blog.csdn.net/weixin_51293984/article/details/128013891