v刹许昌县

package com.example.music;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Media;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity implements OnClickListener {
 List<Object> musiclists = new ArrayList<Object>();
 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 Button play_pause, stop, onplay, downplay, close, exit;
 ActivityReceiver activityReceiver;
 public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION";
 public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION";
 Intent intentservice;
 // 定义音乐的播放状态 ,0X11 代表停止 ,0x12代表播放,0x13代表暂停
 int status = 0x11;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  UIinit();
  logic();
  musicList();
  activityReceiver = new ActivityReceiver();
  IntentFilter filter = new IntentFilter();
  filter.addAction(UPDATE_ACTION);
  registerReceiver(activityReceiver, filter);
  intentservice = new Intent(this, MusicService.class);
  startService(intentservice);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 public void UIinit() {
  play_pause = (Button) this.findViewById(R.id.play_pause);
  stop = (Button) this.findViewById(R.id.stop);
  onplay = (Button) this.findViewById(R.id.onplay);
  close = (Button) this.findViewById(R.id.close);
  exit = (Button) this.findViewById(R.id.exit);
  downplay = (Button) this.findViewById(R.id.downplay);
 }

 public void logic() {
  play_pause.setOnClickListener(this);
  stop.setOnClickListener(this);
  onplay.setOnClickListener(this);
  downplay.setOnClickListener(this);
  close.setOnClickListener(this);
  exit.setOnClickListener(this);
 }

 @Override
 public void onClick(View source) {
  Intent intent = new Intent(CTL_ACTION);
  switch (source.getId()) {
  case R.id.play_pause: {
   intent.putExtra("control", 1);
   break;
  }
  case R.id.stop: {
   intent.putExtra("control", 2);
   break;
  }
  case R.id.onplay: {
   intent.putExtra("control", 3);
   break;
  }
  case R.id.downplay: {
   intent.putExtra("control", 4);
   break;
  }
  case R.id.close: {
   this.finish();
   break;
  }
  case R.id.exit: {
   stopService(intentservice);
   this.finish();
   break;
  }
  }
  sendBroadcast(intent);

 }

 
 public class ActivityReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
   // 获取Intent中的update消息,update代表播放状态
    int update = intent.getIntExtra("update", -1);
   switch (update) {
   case 0x11: {
    play_pause.setText("播放");
    status = 0x11;
    break;
   }

   // 控制系统进入播放状态
   case 0x12: {
    // 播放状态下设置使用按钮
    play_pause.setText("暂停");
    // 设置当前状态
    status = 0x12;
    break;
   }
   // 控制系统进入暂停状态
   case 0x13: {
    play_pause.setText("播放");
    status = 0x13;
    break;
   }
   }
  }

 }

 /* 播放列表 */
 public void musicList() {
  // 取得指定位置的文件设置显示到播放列表
  String[] music = new String[] { Media._ID, Media.DISPLAY_NAME,
    Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA };
  Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
    music, null, null, null);
  while (cursor.moveToNext()) {
   Music temp = new Music();
   temp.setFilename(cursor.getString(1));
   temp.setTitle(cursor.getString(2));
   temp.setDuration(cursor.getInt(3));
   temp.setArtist(cursor.getString(4));
   temp.setData(cursor.getString(5));
   musiclists.add(temp);

   Map<String, Object> map = new HashMap<String, Object>();
   map.put("name", cursor.getString(1));
   map.put("artist", cursor.getString(4));
   list.add(map);
  }

  ListView listview = (ListView) findViewById(R.id.musics);
  SimpleAdapter adapter = new SimpleAdapter(this, list,
    R.layout.musicsshow, new String[] { "name", "artist" },
    new int[] { R.id.name, R.id.artist });
  listview.setAdapter(adapter);
  listview.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int current, long id) {

    Intent intent=new Intent(CTL_ACTION);
    intent.putExtra("control", 5);
    intent.putExtra("current", current);
    sendBroadcast(intent);
   }
  });
 }
}

 

 

 

 

package com.example.music;


public class Music {
 
 private String filename;
 private String Title;
 private int   duration;
 private String artist;
 private String location;
 
 
 public String getFilename() {
  return filename;
 }
 public void setFilename(String filename) {
  this.filename = filename;
 }
 public String getTitle() {
  return Title;
 }
 public void setTitle(String title) {
  Title = title;
 }
 public int getDuration() {
  return duration;
 }
 public void setDuration(int duration) {
  this.duration = duration;
 }
 public String getArtist() {
  return artist;
 }
 public void setArtist(String artist) {
  this.artist = artist;
 }
 public String getData() {
  return location;
 }
 public void setData(String location) {
  this.location = location;
 }
 
 
 
// {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME,
//  MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION,
//  MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA
}

 

 

 

package com.example.music;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.provider.MediaStore.Audio.Media;
import android.util.Log;


public class MusicService extends Service {
 List<Object> musiclists = new ArrayList<Object>();
 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 MyReceiver serviceReceiver;
 MediaPlayer mPlayer;
 int status = 0x11;
 //歌曲下标
 int current = 0;
 int count = 0;
    int flog = 0;
    //String filename;
 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public void onDestroy() {
  flog=0;
  mPlayer.stop();
  mPlayer.release();
  super.onDestroy();
 }

 @Override
 public void onCreate() {
  flog=1;
  musicList();
  count = list.size();
  Log.e("Service", "onStart");
  serviceReceiver = new MyReceiver();
  IntentFilter filter = new IntentFilter();
  filter.addAction(MainActivity.CTL_ACTION);
  registerReceiver(serviceReceiver, filter);
  mPlayer = new MediaPlayer();
  mPlayer.setOnCompletionListener(new OnCompletionListener() {
   @Override
   public void onCompletion(MediaPlayer mp) {
    current++;
    if (current >= count) {
     current = 0;
    }
    String filename = ((Music) musiclists.get(current)).getData();
    playMusic(filename);
   }
  });
  
  super.onCreate();
 }

 
 @Override
 public void onStart(Intent intent, int startId) { 
  if(flog==2){
   Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
   sendIntent.putExtra("update", status);
   //sendIntent.putExtra("current", current);
   sendBroadcast(sendIntent);
  }
  flog=2;
 }


 
 
 
  private void playMusic(String path)  
     {  
         try  
         {  
             /* 重置MediaPlayer */  
          mPlayer.reset();  
             /* 设置要播放的文件的路径 */  
          mPlayer.setDataSource(path);  
             /* 准备播放 */  
          mPlayer.prepare();  
             /* 开始播放 */  
          mPlayer.start();  
          mPlayer.setOnCompletionListener(new OnCompletionListener()   
             {  
                 public void onCompletion(MediaPlayer arg0)  
                 {  
                     //播放完成一首之后进行下一首  
                  current--;
         if (current < 0) {
          current = count;
         }
         playMusic( ((Music) musiclists.get(current)).getData());
         status = 0x12;
                 }  
             });  
         }catch (IOException e){}  
     }  
  
  /* 播放列表 */
  public void musicList() {
   // 取得指定位置的文件设置显示到播放列表
   String[] music = new String[] { Media._ID, Media.DISPLAY_NAME,
     Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA };
   Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
     music, null, null, null);
   while (cursor.moveToNext()) {
    Music temp = new Music();
    temp.setFilename(cursor.getString(1));
    temp.setTitle(cursor.getString(2));
    temp.setDuration(cursor.getInt(3));
    temp.setArtist(cursor.getString(4));
    temp.setData(cursor.getString(5));
    musiclists.add(temp);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", cursor.getString(1));
    map.put("artist", cursor.getString(4));
    list.add(map);
   }
  }
 
 public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
   int control = intent.getIntExtra("control", -1);
   switch (control) {
   case 1: {
    // 如果原来处于停止的状态
    if (status == 0x11) {
     playMusic( ((Music) musiclists.get(current)).getData());
     status = 0x12;
    }
    // 原来处于播放状态
    else if (status == 0x12) {
     mPlayer.pause();
     status = 0x13;
    }
    // 原来处于暂停状态
    else if (status == 0x13) {
     mPlayer.start();
     status = 0x12;
    }
    break;
   }
   case 2: {
    // 如果原来正在播放或暂停
    if (status == 0x12 || status == 0x13) {
     mPlayer.stop();
     status = 0x11;
    }
    break;
   }
   case 3: {
    current--;
    if (current < 0) {
     current = count;
    }
    playMusic( ((Music) musiclists.get(current)).getData());
    status = 0x12;
    break;
   }
   case 4: {
    current++;
    if (current > count) {
     current = 0;
    }
    playMusic( ((Music) musiclists.get(current)).getData());
    status = 0x12;
    break;
   }
   case 5: {
    current = intent.getIntExtra("current", -1);;
    playMusic( ((Music) musiclists.get(current)).getData());
    status = 0x12;
    break;
   }
   }
   Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
   sendIntent.putExtra("update", status);
  // sendIntent.putExtra("current", current);
   sendBroadcast(sendIntent);
  }

 }

 
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值