Android之SoundPool背景音乐,MediaPlayer播放背景音乐

本文介绍了一种在Android应用中实现音视频播放的方法,通过使用SoundPool和MediaPlayer类,实现了短音频的循环播放及背景音乐的控制。文章详细展示了如何初始化播放器、设置音量以及播放、暂停和停止媒体文件。
摘要由CSDN通过智能技术生成
public class SimpleActivity extends AppCompatActivity {
    SoundPool sp;
    HashMap<Integer, Integer> hm;
    int currStreanId;
    @Bind(R.id.button2)
    Button button2;
    @Bind(R.id.button3)
    Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        ButterKnife.bind(this);

        initSoundPool();

    }



    @OnClick({R.id.button2, R.id.button3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button2:
                playSoud(1,-1);//播放次数,-1表示无限重复,0表示一次,1表示两次,以此类推。
                break;
            case R.id.button3:
                sp.stop(currStreanId);
                break;
        }
    }

    /**
     * 由于SoundPool只适合播放不大于7秒的文件,实际游戏开发中采用的是MediaPlay来播放背景音乐
     * 
     * 
     */

//初始化声音池
    private void initSoundPool() {
        sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
        hm = new HashMap<>();
        hm.put(1, sp.load(this, R.raw.siml, 1));

    }
    //播放声音的方法
    private void playSoud(int sound, int loop) {

        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;//计算得到的播放音量
        currStreanId = sp.play(hm.get(sound), volume, volume, 1, loop, 1.0f);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.yang.yin.yinyangapplication.SimpleActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/playmu"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="130dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stopmu"
        android:id="@+id/button3"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

这里写图片描述


MediaPlayer播放背景音乐

这里写图片描述

public class SimpleActivity extends AppCompatActivity {


    @Bind(R.id.button2)
    Button button2;
    @Bind(R.id.button3)
    Button button3;
    @Bind(R.id.button4)
    Button button4;
    @Bind(R.id.button5)
    Button button5;
    @Bind(R.id.button6)
    Button button6;

    MediaPlayer mp;
    AudioManager am;

    private int maxVolume;
    private int currVolume;
    private  int stepVolume;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        ButterKnife.bind(this);


        mp = MediaPlayer.create(this, R.raw.siml);


        am = (AudioManager) this.getSystemService(AUDIO_SERVICE);
          maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        stepVolume = maxVolume/6;



    }

    @OnClick({R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button2:
                mp.start();
                break;
            case R.id.button3:
                mp.pause();
                break;
            case R.id.button4:
                mp.stop();
                try {
                    mp.prepare();//进入准备状态
                } catch (IOException e) {
                    e.printStackTrace();
                }

                break;
            case R.id.button5:
                currVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
                int tmpVolume = currVolume + stepVolume;//临时音量
                currVolume = tmpVolume<maxVolume?tmpVolume:maxVolume;
                am.setStreamVolume(AudioManager.STREAM_MUSIC,currVolume,AudioManager.FLAG_PLAY_SOUND);
                break;
            case R.id.button6:
                currVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
                int tmVolume = currVolume - stepVolume;//临时音量
                currVolume = tmVolume<0?tmVolume:0;
                am.setStreamVolume(AudioManager.STREAM_MUSIC,currVolume,AudioManager.FLAG_PLAY_SOUND);
                break;
        }
    }
}

这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.yang.yin.yinyangapplication.SimpleActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/playmu"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="130dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stopmu"
        android:id="@+id/button3"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/closemu"
        android:id="@+id/button4"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_sound"
        android:id="@+id/button5"
        android:layout_below="@+id/button4"
        android:layout_alignRight="@+id/button3"
        android:layout_alignEnd="@+id/button3"
        android:layout_marginTop="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/small_sound"
        android:id="@+id/button6"
        android:layout_below="@+id/button5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="24dp" />
</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值