android MediaPlayer 简易播放器的实现 及类似斗地主音频池实现

一:android MediaPlayer 简易播放器的实现
(一)MediaPlayer实现前提
1.音频来源途径(应用内,内存卡中的音频,网络上)

(二)代码块实现
public class MediaplayerActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mediaplayer);

        seekBar = (SeekBar) findViewById(R.id.seekbar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //获取拖动结束之后的位置
                int progress = seekBar.getProgress();
                //跳转到某个位置播放
                mediaPlayer.seekTo(progress);
            }
        });
    }

    public void isPlayOrPause(View view) {
        final ImageButton imageButton = (ImageButton) view;

        if (mediaPlayer == null) {
            //实例化MediaPlayer
            //1.播放应用内
//        mediaPlayer = MediaPlayer.create(this, R.raw.love);
            //2.播放内存卡中音频文件
            mediaPlayer = new MediaPlayer();
            //设置类型
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            //设置音源
            try {

//                mediaPlayer.setDataSource(this, Uri.parse("file://netease/cloudmusic/Music/mylove.mp3"));
                //3.播放网络音乐
                mediaPlayer.setDataSource(this, Uri.parse("http://192.168.43.244:8080/dbqq.mp3"));

                //准备一下(内存卡)
//                mediaPlayer.prepare();

                //准备(网络)
                mediaPlayer.prepareAsync();

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            //把图标修改为播放的图标
            imageButton.setImageResource(android.R.drawable.ic_media_play);
        } else {
            mediaPlayer.start();
            //把图标修改为暂停的图标
            imageButton.setImageResource(android.R.drawable.ic_media_pause);
        }
        //监听:准备完成的监听
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
                //把图标修改为暂停的图标
                imageButton.setImageResource(android.R.drawable.ic_media_pause);
                //获取音乐的总时长
                int duration = mediaPlayer.getDuration();
                //设置进度条的最大值为音乐的总时长
                seekBar.setMax(duration);
                new MyThread().start();
            }
        });

    }

    //开启线程
    class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (seekBar.getProgress() <= seekBar.getMax()) {
                //获取当前音乐播放的位置
                int currentPosition = mediaPlayer.getCurrentPosition();

                //让进度条动起来
                seekBar.setProgress(currentPosition);
            }
        }
    }
}

 
  
(三)注意点
 
  

<uses-permission android:name="android.permission.INTERNET" />

二:android 类似斗地主音频池实现
public class PlayAnimalActivity extends AppCompatActivity {

    private SoundPool soundPool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_animal);

        //实例化音频池
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC,0);

        //给音频池设置加载完成的监听
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int i, int i1) {
                soundPool.play(i,1,1,1,100,1);
            }
        });
    }

    public void playKFC(View view){
        soundPool.load(this,R.raw.rooster,1);
    }
    public void playTwo(View view){
        soundPool.load(this,R.raw.chimp,1);
    }
    public void playThree(View view){
        soundPool.load(this,R.raw.crickets,1);
    }
    public void playFour(View view){
        soundPool.load(this,R.raw.roar,1);
    }
    public void playDog(View view){
        soundPool.load(this,R.raw.dogbark,1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(soundPool!=null){
            soundPool.release();
            soundPool=null;
        }
    }
}


你可能感兴趣的:(android)