A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shenzhinishimen 中级黑马   /  2016-6-12 00:45  /  372 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Android 中soundpool的应用:
实现效果:
            点击暂停 音乐暂停
            点击开始  音乐播放
            当复选按钮选择之后 播放背景音乐,取消之后没有背景音乐
MainActivity.java  代码:



import java.io.IOException;
import java.util.HashMap;


import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;


public class MainActivity extends Activity {
        private Button bt1, bt2;
        private CheckBox cb1;
        private MediaPlayer player;
        private SoundPool sp;
        private ImageView iv1;
        private HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                bt1 = (Button) findViewById(R.id.bt1);
                bt2 = (Button) findViewById(R.id.bt2);
                cb1 = (CheckBox) findViewById(R.id.cb1);
                iv1 = (ImageView) findViewById(R.id.iv1);
                player = new MediaPlayer();
                String path = "/sdcard/b.mp3";
                try {
                        player.setDataSource(path);
                        player.prepare();
                } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (SecurityException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                player.setLooping(true);
                player.start();


                final Animation a = AnimationUtils.loadAnimation(MainActivity.this,
                                R.anim.dh);
                sp = new SoundPool(5, AudioManager.STREAM_SYSTEM, 0);
                hm.put(1, sp.load(MainActivity.this, R.raw.c, 0));
                cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {


                        @Override
                        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                                // TODO Auto-generated method stub
                                if (!cb1.isChecked()) {
                                        player.pause();
                                } else
                                        player.start();
                        }


                });
                bt2.setOnClickListener(new OnClickListener() {


                        @Override
                        public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                iv1.setVisibility(View.INVISIBLE);
                                iv1.startAnimation(a);
                                a.setAnimationListener(new AnimationListener() {


                                        @Override
                                        public void onAnimationStart(Animation arg0) {
                                                // TODO Auto-generated method stub
                                                sp.play(hm.get(1), 1, 1, 0, -1, 1);
                                        }


                                        @Override
                                        public void onAnimationRepeat(Animation arg0) {
                                                // TODO Auto-generated method stub
                                                sp.play(hm.get(1), 1, 1, 0, -1, 1);
                                        }


                                        @Override
                                        public void onAnimationEnd(Animation arg0) {
                                                // TODO Auto-generated method stub


                                        }
                                });
                        }
                });
        }


        protected void onPause() {
                super.onPause();
                player.release();
                sp.release();
        }
}
activity_main.xml   代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="暂停" />


    <Button
        android:id="@+id/bt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt1"
        android:text="开始" />


    <CheckBox
        android:id="@+id/cb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt2"
        android:layout_centerHorizontal="true"
        android:text="背景音乐" />


    <ImageView
        android:id="@+id/iv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="30dp"
        android:background="@drawable/ic_launcher"
        android:visibility="invisible" />


</RelativeLayout>

QQ图片20160612004058.png (4.63 KB, 下载次数: 2)

QQ图片20160612004058.png

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马