音频音效Audio

移动开发
音频声效Audio,适合拿来学习

源码简介:音频声效Audio,适合拿来学习

源码效果:

源码片段:

  1. package com.example.shiyan; 
  2.   
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5. import android.app.Service; 
  6. import android.content.Intent; 
  7. import android.graphics.Bitmap; 
  8. import android.graphics.BitmapFactory; 
  9. import android.graphics.drawable.BitmapDrawable; 
  10. import android.media.MediaPlayer; 
  11. import android.media.MediaPlayer.OnCompletionListener; 
  12. import android.net.Uri; 
  13. import android.os.Handler; 
  14. import android.os.IBinder; 
  15. import android.view.animation.AnimationUtils; 
  16. import android.widget.Toast; 
  17.   
  18. /** 
  19.  * 类说明:音乐播放服务类 
  20.  * @author LiangAn 
  21.  * @version 创建时间:2015年3月18日 上午10:24:17 
  22.  */ 
  23. public class ControlPlay extends Service { 
  24.   
  25.     public static MediaPlayer myMediaPlayer; 
  26.   
  27.     public static MainActivity c_ma; 
  28.   
  29.     public static int playing_id = 0
  30.   
  31.     // 初始化歌曲播放时间的变量 
  32.     private int CurrentTime = 0
  33.     // 初始化歌曲总时间的变量 
  34.     private int CountTime = 0
  35.   
  36.     public static Music_infoAdapter m_in; 
  37.   
  38.     public Thread thread; 
  39.     Handler handler = new Handler(); 
  40.     public boolean playFlag = true
  41.     public int vTemp = 0
  42.       
  43.   
  44.     @Override 
  45.     public IBinder onBind(Intent intent) { 
  46.         return null
  47.     } 
  48.   
  49.     @Override 
  50.     public void onCreate() { 
  51.         super.onCreate(); 
  52.         initMediaSource(initMusicUri(0)); 
  53.     } 
  54.   
  55.     @Override 
  56.     public void onDestroy() { 
  57.         super.onDestroy(); 
  58.         if (myMediaPlayer != null) { 
  59.             myMediaPlayer.stop(); 
  60.             myMediaPlayer.release(); 
  61.             myMediaPlayer = null
  62.         } 
  63.     } 
  64.   
  65.     @Override 
  66.     public void onStart(Intent intent, int startId) { 
  67.         super.onStart(intent, startId); 
  68.         String playFlag = intent.getExtras().getString("control"); 
  69.         if ("play".equals(playFlag)) { 
  70.             playMusic(); 
  71.         } else if ("next".equals(playFlag)) { 
  72.             playNext(); 
  73.         } else if ("front".equals(playFlag)) { 
  74.             playFront(); 
  75.         } else if ("listClick".equals(playFlag)) { 
  76.             playing_id = intent.getExtras().getInt("musicId_1"); 
  77.             initMediaSource(initMusicUri(playing_id)); 
  78.             playMusic(); 
  79.         } else if ("gridClick".equals(playFlag)) { 
  80.             playing_id = intent.getExtras().getInt("musicId_2"); 
  81.             initMediaSource(initMusicUri(playing_id)); 
  82.             playMusic(); 
  83.         } 
  84.     } 
  85.   
  86.     /** 
  87.      * 初始化媒体对象 
  88.      *  
  89.      * @param mp3Path 
  90.      *            mp3路径 
  91.      */ 
  92.     public void initMediaSource(String mp3Path) { 
  93.         Uri mp3Uri = Uri.parse(mp3Path); 
  94.         if (myMediaPlayer != null) { 
  95.             myMediaPlayer.stop(); 
  96.             myMediaPlayer.reset(); 
  97.             myMediaPlayer = null
  98.         } 
  99.         // 为myMediaPlayer创建对象 
  100.         myMediaPlayer = MediaPlayer.create(this, mp3Uri); 
  101.         //myMediaPlayer = MediaPlayer.create(this, R.raw.aaaa); 
  102.     //  c_ma.startRhythm(myMediaPlayer); 
  103.     } 
  104.   
  105.     /** 
  106.      * 返回列表第几行的歌曲路径 
  107.      *  
  108.      * @param _id 
  109.      *            表示歌曲序号,从0开始 
  110.      */ 
  111.     public String initMusicUri(int _id) { 
  112.         playing_id = _id; 
  113.         String s; 
  114.         // 判断列表和列表长度不为空时才获取 
  115.         if (Music_infoAdapter.musicList != null 
  116.                 && Music_infoAdapter.musicList.size() != 0) { 
  117.             s = Music_infoAdapter.musicList.get(_id).getMusicPath(); 
  118.             return s; 
  119.         } else { 
  120.             // 否则返回空字符串 
  121.             return ""
  122.         } 
  123.     } 
  124.     /** 
  125.      * 音乐播放方法,并且带有暂停方法 
  126.      */ 
  127.     public void playMusic() { 
  128.   
  129.         // 判断歌曲不能为空 
  130. System.out.println("sfajfdsa"); 
  131.         if (myMediaPlayer != null && Music_infoAdapter.musicList.size() != 0) { 
  132.             if (myMediaPlayer.isPlaying()) { 
  133.                 // 歌曲暂停 
  134.                 myMediaPlayer.stop(); 
  135.                 // 更换播放按钮背景 
  136.                 MainActivity.play_ImageButton 
  137.                         .setBackgroundResource(R.drawable.play_button); 
  138.                 // 取消通知 
  139.                 MainActivity.mNotificationManager.cancel(1); 
  140.             } else { 
  141.                 System.out.println("sfajfdsa2"); 
  142.         //      myMediaPlayer.start(); 
  143.                 try {        
  144.                      if(myMediaPlayer != null)   
  145.                      {   
  146.                          myMediaPlayer.stop();   
  147.                      }       
  148.                      myMediaPlayer.prepare();   
  149.                      myMediaPlayer.start();   
  150.                       
  151.                     } catch (Exception e) {   
  152.                      
  153.                      e.printStackTrace();   
  154.                     } 
  155.                 // /////////////////////// 初始化歌词配置 /////////////////////// // 
  156.   
  157.                 // 更换背景- 
  158.                 MainActivity.play_ImageButton 
  159.                         .setBackgroundResource(R.drawable.pause_button); 
  160.                 // 启动线程更新SeekBar 
  161.                 startSeekBarUpdate(); 
  162.                   
  163.                 // 更新歌曲播放第几首 
  164.                 int x = playing_id + 1
  165.                 MainActivity.music_number.setText("" + x + "/" 
  166.                         + Music_infoAdapter.musicList.size()); 
  167.   
  168.                 // 截取.mp3字符串 
  169.                 String a = Music_infoAdapter.musicList.get(playing_id) 
  170.                         .getMusicName(); 
  171. //              int b = a.indexOf(".mp3"); 
  172. //              String c = a.substring(0, b); 
  173.                 // 切换带动画更新歌曲名 
  174.                 MainActivity.music_Name.setText(a); 
  175.                 MainActivity.music_Name.setAnimation(AnimationUtils 
  176.                         .loadAnimation(ControlPlay.this, R.anim.translate_z)); 
  177.   
  178.                 // 带动画更新专辑名 
  179.                 MainActivity.music_Album 
  180.                         .setText(Music_infoAdapter.musicList.get(playing_id) 
  181.                                 .getMusicAlbum()); 
  182.                 MainActivity.music_Album.setAnimation(AnimationUtils 
  183.                         .loadAnimation(ControlPlay.this, R.anim.alpha_y)); 
  184.   
  185.                 // 更新歌曲时间 
  186.                 MainActivity.time_right.setText(Music_infoAdapter 
  187.                         .toTime(Music_infoAdapter.musicList.get(playing_id) 
  188.                                 .getMusicTime())); 
  189.   
  190.             } 
  191.   
  192.             /** 
  193.              * 监听播放是否完成 
  194.              */ 
  195.             myMediaPlayer.setOnCompletionListener(new OnCompletionListener() { 
  196.   
  197.                 @Override 
  198.                 public void onCompletion(MediaPlayer mp) { 
  199.                     // TODO Auto-generated method stub 
  200.   
  201.                     // 播放完当前歌曲,自动播放下一首 
  202.                     playNext(); 
  203.                 } 
  204.             }); 
  205.   
  206.         } else { 
  207.             Toast.makeText(ControlPlay.this"木有在手机里找到歌曲啊..."
  208.                     Toast.LENGTH_SHORT).show(); 
  209.         } 
  210.     } 
  211.   
  212.     /** 
  213.      * 播放下一首 
  214.      */ 
  215.     public void playNext() { 
  216.   
  217.         // 判断歌曲不能为空 
  218.         if (Music_infoAdapter.musicList.size() != 0) { 
  219.             // 如果到了***一首则一直播放***一首 
  220.             if (playing_id == Music_infoAdapter.musicList.size() - 1) { 
  221.                 playing_id = Music_infoAdapter.musicList.size() - 1
  222.                 Toast.makeText(ControlPlay.this"已经是***一首啦!"
  223.                         Toast.LENGTH_SHORT).show(); 
  224.   
  225.                 MainActivity.play_ImageButton 
  226.                         .setBackgroundResource(R.drawable.play_button); 
  227.                 MainActivity.mNotificationManager.cancel(1); 
  228.   
  229.             } else { 
  230.                 initMediaSource(initMusicUri(++playing_id)); 
  231.                 playMusic(); 
  232.             } 
  233.         } else { 
  234.             Toast.makeText(ControlPlay.this"木有找到歌曲啊!", Toast.LENGTH_SHORT) 
  235.                     .show(); 
  236.         } 
  237.     } 
  238.   
  239.     /** 
  240.      * 播放上一首 
  241.      */ 
  242.     public void playFront() { 
  243.   
  244.         // 判断歌曲不能为空 
  245.         if (Music_infoAdapter.musicList.size() != 0) { 
  246.             // 如果到了***首则一直播放***首 
  247.             if (playing_id == 0) { 
  248.                 playing_id = 0
  249.                 Toast.makeText(ControlPlay.this"现在就是***首哦!"
  250.                         Toast.LENGTH_SHORT).show(); 
  251.             } else { 
  252.                 initMediaSource(initMusicUri(--playing_id)); 
  253.                 playMusic(); 
  254.             } 
  255.         } else { 
  256.             Toast.makeText(ControlPlay.this"木有找到歌曲啊!", Toast.LENGTH_SHORT) 
  257.                     .show(); 
  258.         } 
  259.     } 
  260.   
  261.     public void startSeekBarUpdate() { 
  262.         handler.post(start); 
  263.     } 
  264.   
  265.     Runnable start = new Runnable() { 
  266.   
  267.         @Override 
  268.         public void run() { 
  269.             // TODO Auto-generated method stub 
  270.   
  271.             handler.post(updatesb); 
  272.         } 
  273.   
  274.     }; 
  275.   
  276.     Runnable updatesb = new Runnable() { 
  277.   
  278.         @Override 
  279.         public void run() { 
  280.             // 获取SeekBar走动到那的时间 
  281.             MainActivity.play_time = myMediaPlayer 
  282.                     .getCurrentPosition(); 
  283.   
  284.             // 设置填充当前获取的进度 
  285.             MainActivity.seekbar 
  286.                     .setProgress(MainActivity.play_time); 
  287.             // SeekBar的***值填充歌曲时间 
  288.             MainActivity.seekbar.setMax(Music_infoAdapter.musicList 
  289.                     .get(playing_id).getMusicTime()); 
  290.   
  291.             // 线程延迟1000毫秒启动 
  292.             handler.postDelayed(updatesb, 1000); 
  293.         } 
  294.     }; 
  295.   
  296.     Handler mHandler = new Handler(); 
  297.       
  298.   
  299.       

下载地址:http://down.51cto.com/data/2111747

责任编辑:倪明 来源: devstore
相关推荐

2023-03-07 15:46:20

鸿蒙音频渲染

2011-06-13 16:22:49

Core AudioCocoa TouchiOS

2017-08-09 15:57:11

JavaScriptHtml5音频

2012-12-24 13:23:26

iOS音频声效源码

2023-02-09 15:37:04

AI技术

2022-11-14 08:26:40

系统音频API

2023-03-22 08:01:04

Androidapi播放

2023-08-04 17:33:27

Meta音频AI

2013-11-14 11:18:47

AndroidAudio

2021-08-09 14:00:24

微软Windows 11Windows

2021-11-22 11:32:00

智能音频汽车

2024-07-19 14:08:02

2013-11-14 13:27:38

AndroidAudio

2011-06-07 15:38:29

2016-10-31 20:21:05

LinuxOctave音频文件

2023-01-05 16:51:04

机器学习人工智能

2012-12-24 09:04:04

iOSUnity3D

2024-06-20 13:44:48

OpenAIV2A模型

2024-02-05 13:28:14

AI音频虚拟人像

2010-01-05 13:36:01

Windows 7音量调整
点赞
收藏

51CTO技术栈公众号