Source code introduction: Audio sound effects Audio, suitable for learning Source code effect: Source code snippet: - 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;
-
-
-
-
-
-
- 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();
- }
- }
-
-
-
-
-
-
-
- public void initMediaSource(String mp3Path) {
- Uri mp3Uri = Uri.parse(mp3Path);
- if (myMediaPlayer != null ) {
- myMediaPlayer.stop();
- myMediaPlayer.reset();
- myMediaPlayer = null ;
- }
-
- myMediaPlayer = MediaPlayer.create( this , mp3Uri);
-
-
- }
-
-
-
-
-
-
-
- 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" );
-
- try {
- if (myMediaPlayer != null )
- {
- myMediaPlayer.stop();
- }
- myMediaPlayer.prepare();
- myMediaPlayer.start();
-
- } catch (Exception e) {
-
- e.printStackTrace();
- }
-
-
-
- MainActivity.play_ImageButton
- .setBackgroundResource(R.drawable.pause_button);
-
- startSeekBarUpdate();
-
-
- int x = playing_id + 1 ;
- MainActivity.music_number.setText( "" + x + "/"
- + Music_infoAdapter.musicList.size());
-
-
- String a = Music_infoAdapter.musicList.get(playing_id)
- .getMusicName();
-
-
-
- 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) {
-
-
-
- playNext();
- }
- });
-
- } else {
- Toast.makeText(ControlPlay. this , "I can't find any songs on my phone..." ,
- 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 , "It's the last song!" ,
- 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 , "No songs found!" , Toast.LENGTH_SHORT)
- .show();
- }
- }
-
-
-
-
- public void playFront() {
-
-
- if (Music_infoAdapter.musicList.size() != 0 ) {
-
- if (playing_id == 0 ) {
- playing_id = 0 ;
- Toast.makeText(ControlPlay. this , "Now it's the first song!" ,
- Toast.LENGTH_SHORT).show();
- } else {
- initMediaSource(initMusicUri(--playing_id));
- playMusic();
- }
- } else {
- Toast.makeText(ControlPlay. this , "No songs found!" , Toast.LENGTH_SHORT)
- .show();
- }
- }
-
- public void startSeekBarUpdate() {
- handler.post(start);
- }
-
- Runnable start = new Runnable() {
-
- @Override
- public void run() {
-
-
- handler.post(updatesb);
- }
-
- };
-
- Runnable updatesb = new Runnable() {
-
- @Override
- public void run() {
-
- MainActivity.play_time = myMediaPlayer
- .getCurrentPosition();
-
-
- MainActivity.seekbar
- .setProgress(MainActivity.play_time);
-
- MainActivity.seekbar.setMax(Music_infoAdapter.musicList
- .get(playing_id).getMusicTime());
-
-
- handler.postDelayed(updatesb, 1000 );
- }
- };
-
- Handler mHandler = new Handler();
-
-
-
- }
Download address: http://download..com/data/2111747 |