Android Open Source - 5th Player Services






From Project

Back to project page 5th.

License

The source code is released under:

GNU General Public License

If you think the Android project 5th listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

?
package com.zhanglin.practice;
//from  w w w  . j a v  a  2s  .c  o  m
import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.util.Log;

public class PlayerServices extends Service
{
  public static MediaPlayer myMediaPlayer=new MediaPlayer();// ????
  private int isfirst=0;
  static Random random=new Random();

  @Override
  public void onCreate()
  {
    super.onCreate();
    isfirst=1;
    try
    {
      myMediaPlayer.reset();
      myMediaPlayer.setDataSource(PublicList.path[PublicList.currentItem]);
      myMediaPlayer.prepare();
    }
    catch(Exception e)
    {
      // TODO: handle exception
      e.printStackTrace();
      Log.i("playing","Err");
    }
  }

  @Override
  public void onStart(Intent intent,int startId)
  {
    super.onStart(intent,startId);    
    if(isfirst!=1)
    {
      //playMusic(PublicList.path[PublicList.currentItem]);
    }
    isfirst=0;
  }


  @Override
  public void onDestroy()
  {
    myMediaPlayer.stop();
    super.onDestroy();
  }

  @Override
  public IBinder onBind(Intent arg0)
  {
    return null;
  }
  
  static void playMusic(String path)
  {
    Log.i("playing","playMusic");
    Log.i("playing",path);
    try
    {
      myMediaPlayer.reset();
      myMediaPlayer.setDataSource(path);
      Log.i("playing","playMusic-->try");
      myMediaPlayer.prepare();
      myMediaPlayer.start();
      myMediaPlayer.setOnCompletionListener(new OnCompletionListener()
      {

        @Override
        public void onCompletion(MediaPlayer mp)
        {
          // TODO Auto-generated method stub
          switch(SetModeActivity.mode)
          {
          case 1:
            if(++PublicList.currentItem>PublicList.size)//if ????--????currentItem?????
            {
              PublicList.currentItem=0;
            }
            playMusic(PublicList.path[PublicList.currentItem]);
            break;
          case 2:
            playMusic(PublicList.path[PublicList.currentItem]);
            break;
          case 3:
            PublicList.currentItem=random.nextInt(PublicList.size);
            playMusic(PublicList.path[PublicList.currentItem]);
            break;
          default:
            break;
          }          
          //nextMusic();
          PlayingActivity.seekbar.setMax(PlayerServices.myMediaPlayer.getDuration());
          PlayingActivity.completion="true";
          
          
        }
      });
    }
    catch(Exception e)
    {
      // TODO: handle exception
      e.printStackTrace();
      Log.i("playing","Err");
    }
  }

  static void lastMusic()
  {
    switch(SetModeActivity.mode)
    {
    case 1:
      if(--PublicList.currentItem<0)//if ????--????currentItem?????
      {
        PublicList.currentItem=PublicList.size;
      }
      playMusic(PublicList.path[PublicList.currentItem]);
      break;
    case 2:
      playMusic(PublicList.path[PublicList.currentItem]);
      break;
    case 3:
      PublicList.currentItem=random.nextInt(PublicList.size);
      playMusic(PublicList.path[PublicList.currentItem]);
      break;

    default:
      break;
    }
  }

  static void nextMusic()
  {
    switch(SetModeActivity.mode)
    {
    case 1:
      if(++PublicList.currentItem>PublicList.size)
      {
        PublicList.currentItem=0;
      }
      playMusic(PublicList.path[PublicList.currentItem]);
      break;
    case 2:
      playMusic(PublicList.path[PublicList.currentItem]);
      break;
    case 3:
      PublicList.currentItem=random.nextInt(PublicList.size);
      playMusic(PublicList.path[PublicList.currentItem]);
      break;

    default:
      break;
    }
  }
  
  public void addNotify()
  {
    String ns=Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager=(NotificationManager)getSystemService(ns);
    int icon=R.drawable.android;
    CharSequence tickerText=PublicList.title[PublicList.currentItem];
    long when=System.currentTimeMillis();
    
    Notification notification=new Notification(icon,tickerText,when);
    Context context=getApplicationContext();
    CharSequence contentTitle="MusicPlayer";
    CharSequence contentText="?????"+PublicList.title[PublicList.currentItem];//"????????";
    Intent notificationIntent=new Intent(this,PlayingActivity.class);
    PendingIntent contentIntent=PendingIntent.getActivity(this,0,notificationIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
    
    notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
    notification.defaults|=Notification.DEFAULT_LIGHTS;
    
    mNotificationManager.notify(3,notification);
    
  }
  
}




Java Source Code List

.AlbumActivity.java
.MainActivity.java
.PlayListActivity.java
.alb.java
.art.java
com.zhanglin.practice.About.java
com.zhanglin.practice.ArtistActivity.java
com.zhanglin.practice.ChangeGestureDetector.java
com.zhanglin.practice.DataBase.java
com.zhanglin.practice.MusicListAdapter.java
com.zhanglin.practice.PlayerServices.java
com.zhanglin.practice.PlayingActivity.java
com.zhanglin.practice.PublicList.java
com.zhanglin.practice.SetModeActivity.java
com.zhanglin.practice.SongsActivity.java
.formatTime.java