Audio

Audio

Source code introduction: Audio sound effects Audio, suitable for learning

Source code effect:

Source code snippet:

  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. * Class Description: Music playback service class
  20. * @author LiangAn
  21. * @version Created on March 18, 2015 at 10:24:17 AM
  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. // Initialize the variable for the song playing time  
  32. private   int CurrentTime = 0 ;
  33. // Initialize the variable for the total time of the song  
  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. * Initialize the media object
  88. *
  89. * @param mp3Path
  90. * mp3 path
  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. // Create an object for myMediaPlayer  
  100. myMediaPlayer = MediaPlayer.create( this , mp3Uri);
  101. //myMediaPlayer = MediaPlayer.create(this, R.raw.aaaa);  
  102. // c_ma.startRhythm(myMediaPlayer);  
  103. }
  104.   
  105. /**
  106. * Returns the song path of the row in the list
  107. *
  108. * @param _id
  109. * indicates the song number, starting from 0
  110. */  
  111. public String initMusicUri( int _id) {
  112. playing_id = _id;
  113. String s;
  114. // Get the value only when the list and list length are not empty  
  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. // Otherwise returns an empty string  
  121. return   "" ;
  122. }
  123. }
  124. /**
  125. * Music playback method, with pause method
  126. */  
  127. public   void playMusic() {
  128.   
  129. // Check that the song cannot be empty  
  130. System.out.println( "sfajfdsa" );
  131. if (myMediaPlayer != null && Music_infoAdapter.musicList.size() != 0 ) {
  132. if (myMediaPlayer.isPlaying()) {
  133. // Pause the song  
  134. myMediaPlayer.stop();
  135. // Change the play button background  
  136. MainActivity.play_ImageButton
  137. .setBackgroundResource(R.drawable.play_button);
  138. //Cancel notification  
  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. // ///////////////////////// Initialize lyrics configuration //////////////////////// //  
  156.   
  157. // Change background -  
  158. MainActivity.play_ImageButton
  159. .setBackgroundResource(R.drawable.pause_button);
  160. // Start thread to update SeekBar  
  161. startSeekBarUpdate();
  162.                   
  163. // Update the song number to play  
  164. int x = playing_id + 1 ;
  165. MainActivity.music_number.setText( "" + x + "/"  
  166. + Music_infoAdapter.musicList.size());
  167.   
  168. // Intercept .mp3 string  
  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. // Switch to update song name with animation  
  174. MainActivity.music_Name.setText(a);
  175. MainActivity.music_Name.setAnimation(AnimationUtils
  176. .loadAnimation(ControlPlay. this , R.anim.translate_z));
  177.   
  178. // Update album name with animation  
  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. // Update song time  
  186. MainActivity.time_right.setText(Music_infoAdapter
  187. .toTime(Music_infoAdapter.musicList.get(playing_id)
  188. .getMusicTime()));
  189.   
  190. }
  191.   
  192. /**
  193. * Monitor whether the playback is completed
  194. */  
  195. myMediaPlayer.setOnCompletionListener( new OnCompletionListener() {
  196.   
  197. @Override  
  198. public   void onCompletion(MediaPlayer mp) {
  199. // TODO Auto-generated method stub  
  200.   
  201. //After playing the current song, automatically play the next one  
  202. playNext();
  203. }
  204. });
  205.   
  206. } else {
  207. Toast.makeText(ControlPlay. this , "I can't find any songs on my phone..." ,
  208. Toast.LENGTH_SHORT).show();
  209. }
  210. }
  211.   
  212. /**
  213. * Play next song
  214. */  
  215. public   void playNext() {
  216.   
  217. // Check that the song cannot be empty  
  218. if (Music_infoAdapter.musicList.size() != 0 ) {
  219. // If the *** song is played, keep playing the *** song  
  220. if (playing_id == Music_infoAdapter.musicList.size() - 1 ) {
  221. playing_id = Music_infoAdapter.musicList.size() - 1 ;
  222. Toast.makeText(ControlPlay. this , "It's the last song!" ,
  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 , "No songs found!" , Toast.LENGTH_SHORT)
  235. .show();
  236. }
  237. }
  238.   
  239. /**
  240. * Play previous song
  241. */  
  242. public   void playFront() {
  243.   
  244. // Check that the song cannot be empty  
  245. if (Music_infoAdapter.musicList.size() != 0 ) {
  246. // If the ***th song is played, keep playing the ***th song  
  247. if (playing_id == 0 ) {
  248. playing_id = 0 ;
  249. Toast.makeText(ControlPlay. this , "Now it's the first song!" ,
  250. Toast.LENGTH_SHORT).show();
  251. } else {
  252. initMediaSource(initMusicUri(--playing_id));
  253. playMusic();
  254. }
  255. } else {
  256. Toast.makeText(ControlPlay. this , "No songs found!" , 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. // Get the time it takes for SeekBar to move there  
  281. MainActivity.play_time = myMediaPlayer
  282. .getCurrentPosition();
  283.   
  284. // Set the progress of filling the current acquisition  
  285. MainActivity.seekbar
  286. .setProgress(MainActivity.play_time);
  287. //The *** value of SeekBar is filled with song time  
  288. MainActivity.seekbar.setMax(Music_infoAdapter.musicList
  289. .get(playing_id).getMusicTime());
  290.   
  291. // The thread starts after a delay of 1000 milliseconds  
  292. handler.postDelayed(updatesb, 1000 );
  293. }
  294. };
  295.   
  296. Handler mHandler = new Handler();
  297.       
  298.   
  299.       
  300. }

Download address: http://download..com/data/2111747

<<:  The new “League of Legends” for startups

>>:  Scan QR code barcode

Recommend

Is Apple abandoning the iPad mini?

From the iPod to the iPhone to the iPad, Apple ha...

The whole practical process from 0 to 1 growth!

Before I came into contact with the working metho...

Starting from the title: How to mine this gold mine of “TikTok”?

Some people say that short videos are a current b...

The open source game server framework Firefly officially integrates GFirefly!

[[124939]] GFirefly, an important iteration of Fi...

Answers to ASO FAQs, give newbies a tomorrow!

I always feel that I am not a good analytical per...

The founder of YC tells you how to raise funds in more than 10,000 words

[[153077]] Paul Graham (famous programmer, ventur...

Alibaba’s Marketing Strategy Transformation from Tmall’s Double Eleven 2018

In the past two days, I have been studying the 20...

13 golden rules for public relations crisis management!

No business can escape facing a public relations ...

How to operate a content topic and confirm the promotion channels?

A small content topic basically has the following...

How to build an e-commerce product cognition system?

E-commerce is an indispensable industry in our li...