源码简介:音频声效Audio,适合拿来学习
源码效果:
源码片段:
- package com.example.shiyan;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Service;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.net.Uri;
- import android.os.Handler;
- import android.os.IBinder;
- import android.view.animation.AnimationUtils;
- import android.widget.Toast;
- /**
- * 类说明:音乐播放服务类
- * @author LiangAn
- * @version 创建时间:2015年3月18日 上午10:24:17
- */
- public class ControlPlay extends Service {
- public static MediaPlayer myMediaPlayer;
- public static MainActivity c_ma;
- public static int playing_id = 0;
- // 初始化歌曲播放时间的变量
- private int CurrentTime = 0;
- // 初始化歌曲总时间的变量
- private int CountTime = 0;
- public static Music_infoAdapter m_in;
- public Thread thread;
- Handler handler = new Handler();
- public boolean playFlag = true;
- public int vTemp = 0;
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- initMediaSource(initMusicUri(0));
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (myMediaPlayer != null) {
- myMediaPlayer.stop();
- myMediaPlayer.release();
- myMediaPlayer = null;
- }
- }
- @Override
- public void onStart(Intent intent, int startId) {
- super.onStart(intent, startId);
- String playFlag = intent.getExtras().getString("control");
- if ("play".equals(playFlag)) {
- playMusic();
- } else if ("next".equals(playFlag)) {
- playNext();
- } else if ("front".equals(playFlag)) {
- playFront();
- } else if ("listClick".equals(playFlag)) {
- playing_id = intent.getExtras().getInt("musicId_1");
- initMediaSource(initMusicUri(playing_id));
- playMusic();
- } else if ("gridClick".equals(playFlag)) {
- playing_id = intent.getExtras().getInt("musicId_2");
- initMediaSource(initMusicUri(playing_id));
- playMusic();
- }
- }
- /**
- * 初始化媒体对象
- *
- * @param mp3Path
- * mp3路径
- */
- public void initMediaSource(String mp3Path) {
- Uri mp3Uri = Uri.parse(mp3Path);
- if (myMediaPlayer != null) {
- myMediaPlayer.stop();
- myMediaPlayer.reset();
- myMediaPlayer = null;
- }
- // 为myMediaPlayer创建对象
- myMediaPlayer = MediaPlayer.create(this, mp3Uri);
- //myMediaPlayer = MediaPlayer.create(this, R.raw.aaaa);
- // c_ma.startRhythm(myMediaPlayer);
- }
- /**
- * 返回列表第几行的歌曲路径
- *
- * @param _id
- * 表示歌曲序号,从0开始
- */
- public String initMusicUri(int _id) {
- playing_id = _id;
- String s;
- // 判断列表和列表长度不为空时才获取
- if (Music_infoAdapter.musicList != null
- && Music_infoAdapter.musicList.size() != 0) {
- s = Music_infoAdapter.musicList.get(_id).getMusicPath();
- return s;
- } else {
- // 否则返回空字符串
- return "";
- }
- }
- /**
- * 音乐播放方法,并且带有暂停方法
- */
- public void playMusic() {
- // 判断歌曲不能为空
- System.out.println("sfajfdsa");
- if (myMediaPlayer != null && Music_infoAdapter.musicList.size() != 0) {
- if (myMediaPlayer.isPlaying()) {
- // 歌曲暂停
- myMediaPlayer.stop();
- // 更换播放按钮背景
- MainActivity.play_ImageButton
- .setBackgroundResource(R.drawable.play_button);
- // 取消通知
- MainActivity.mNotificationManager.cancel(1);
- } else {
- System.out.println("sfajfdsa2");
- // myMediaPlayer.start();
- try {
- if(myMediaPlayer != null)
- {
- myMediaPlayer.stop();
- }
- myMediaPlayer.prepare();
- myMediaPlayer.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- // /////////////////////// 初始化歌词配置 /////////////////////// //
- // 更换背景-
- MainActivity.play_ImageButton
- .setBackgroundResource(R.drawable.pause_button);
- // 启动线程更新SeekBar
- startSeekBarUpdate();
- // 更新歌曲播放第几首
- int x = playing_id + 1;
- MainActivity.music_number.setText("" + x + "/"
- + Music_infoAdapter.musicList.size());
- // 截取.mp3字符串
- String a = Music_infoAdapter.musicList.get(playing_id)
- .getMusicName();
- // int b = a.indexOf(".mp3");
- // String c = a.substring(0, b);
- // 切换带动画更新歌曲名
- MainActivity.music_Name.setText(a);
- MainActivity.music_Name.setAnimation(AnimationUtils
- .loadAnimation(ControlPlay.this, R.anim.translate_z));
- // 带动画更新专辑名
- MainActivity.music_Album
- .setText(Music_infoAdapter.musicList.get(playing_id)
- .getMusicAlbum());
- MainActivity.music_Album.setAnimation(AnimationUtils
- .loadAnimation(ControlPlay.this, R.anim.alpha_y));
- // 更新歌曲时间
- MainActivity.time_right.setText(Music_infoAdapter
- .toTime(Music_infoAdapter.musicList.get(playing_id)
- .getMusicTime()));
- }
- /**
- * 监听播放是否完成
- */
- myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mp) {
- // TODO Auto-generated method stub
- // 播放完当前歌曲,自动播放下一首
- playNext();
- }
- });
- } else {
- Toast.makeText(ControlPlay.this, "木有在手机里找到歌曲啊...",
- Toast.LENGTH_SHORT).show();
- }
- }
- /**
- * 播放下一首
- */
- public void playNext() {
- // 判断歌曲不能为空
- if (Music_infoAdapter.musicList.size() != 0) {
- // 如果到了***一首则一直播放***一首
- if (playing_id == Music_infoAdapter.musicList.size() - 1) {
- playing_id = Music_infoAdapter.musicList.size() - 1;
- Toast.makeText(ControlPlay.this, "已经是***一首啦!",
- Toast.LENGTH_SHORT).show();
- MainActivity.play_ImageButton
- .setBackgroundResource(R.drawable.play_button);
- MainActivity.mNotificationManager.cancel(1);
- } else {
- initMediaSource(initMusicUri(++playing_id));
- playMusic();
- }
- } else {
- Toast.makeText(ControlPlay.this, "木有找到歌曲啊!", Toast.LENGTH_SHORT)
- .show();
- }
- }
- /**
- * 播放上一首
- */
- public void playFront() {
- // 判断歌曲不能为空
- if (Music_infoAdapter.musicList.size() != 0) {
- // 如果到了***首则一直播放***首
- if (playing_id == 0) {
- playing_id = 0;
- Toast.makeText(ControlPlay.this, "现在就是***首哦!",
- Toast.LENGTH_SHORT).show();
- } else {
- initMediaSource(initMusicUri(--playing_id));
- playMusic();
- }
- } else {
- Toast.makeText(ControlPlay.this, "木有找到歌曲啊!", Toast.LENGTH_SHORT)
- .show();
- }
- }
- public void startSeekBarUpdate() {
- handler.post(start);
- }
- Runnable start = new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- handler.post(updatesb);
- }
- };
- Runnable updatesb = new Runnable() {
- @Override
- public void run() {
- // 获取SeekBar走动到那的时间
- MainActivity.play_time = myMediaPlayer
- .getCurrentPosition();
- // 设置填充当前获取的进度
- MainActivity.seekbar
- .setProgress(MainActivity.play_time);
- // SeekBar的***值填充歌曲时间
- MainActivity.seekbar.setMax(Music_infoAdapter.musicList
- .get(playing_id).getMusicTime());
- // 线程延迟1000毫秒启动
- handler.postDelayed(updatesb, 1000);
- }
- };
- Handler mHandler = new Handler();
- }