package com.zy.zjg; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.zy.player.PlayerService; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.ListView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.SimpleAdapter; import android.widget.TextView; public class Player extends Activity { private ListView listview; private SeekBar mSeekBar; private TextView txtTime; private ArrayList<String> fileNames; private final String FOLDER = Environment.getExternalStorageDirectory() + "/zjg"; private String now = ""; public static boolean playing = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); listview = (ListView) findViewById(R.id.listview); mSeekBar = (SeekBar) findViewById(R.id.seekBar); txtTime = (TextView) findViewById(R.id.txtTime); SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.adapter_list, new String[] { "name" }, new int[] { R.id.txtName }); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { now = FOLDER + "/" + fileNames.get(position); start(); mHandler.sendEmptyMessageDelayed(0, 1000); } }); ((ImageView) findViewById(R.id.btnPlay)).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (playing) { pause(); } else { resume(); } } }); ((ImageView) findViewById(R.id.btnStop)).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { stop(); } }); ((ImageView) findViewById(R.id.btnPrev)).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { } }); ((ImageView) findViewById(R.id.btnNext)).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { } }); mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar arg0) { } @Override public void onStartTrackingTouch(SeekBar arg0) { } @Override public void onProgressChanged(SeekBar arg0, int position, boolean arg2) { int totalDuration = PlayerService.mediaPlayer.getDuration(); seek(totalDuration * position / 100); } }); } Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { int currentPosition = PlayerService.mediaPlayer.getCurrentPosition(); int totalDuration = PlayerService.mediaPlayer.getDuration(); mSeekBar.setProgress(currentPosition * 100 / totalDuration); String time = formatTime(currentPosition); txtTime.setText(time + "/" + formatTime(totalDuration)); if (playing) mHandler.sendEmptyMessageDelayed(0, 1000); else ((ImageView) findViewById(R.id.btnPlay)).setImageResource(R.drawable.play); }; }; private String formatTime(int time) { if (time / 1000 % 60 < 10) { return time / 1000 / 60 + ":0" + time / 1000 % 60; } else { return time / 1000 / 60 + ":" + time / 1000 % 60; } } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); fileNames = getFiles(); Map<String, Object> map = null; if (fileNames.size() > 0) { for (int i = 0; i < fileNames.size(); i++) { map = new HashMap<String, Object>(); map.put("name", fileNames.get(i)); list.add(map); } } return list; } private ArrayList<String> getFiles() { ArrayList<String> list = new ArrayList<String>(); File[] files = null; File dir = new File(FOLDER); if (dir.exists()) { files = dir.listFiles(); } if (files != null) { for (int i = 0; i < files.length; i++) { String fileName = files[i].getName(); if (fileName.endsWith(".mp3")) list.add(files[i].getName()); } } return list; } private void start() { if (now.equals("")) return; playing = true; ((ImageView) findViewById(R.id.btnPlay)).setImageResource(R.drawable.pause); Intent intent = new Intent(Player.this, PlayerService.class); intent.putExtra("option", "start"); intent.putExtra("path", now); startService(intent); } private void resume() { if (now.equals("")) return; playing = true; ((ImageView) findViewById(R.id.btnPlay)).setImageResource(R.drawable.pause); Intent intent = new Intent(Player.this, PlayerService.class); intent.putExtra("option", "resume"); intent.putExtra("path", now); startService(intent); } private void pause() { if (now.equals("")) return; playing = false; ((ImageView) findViewById(R.id.btnPlay)).setImageResource(R.drawable.play); Intent intent = new Intent(Player.this, PlayerService.class); intent.putExtra("option", "pause"); intent.putExtra("path", now); startService(intent); } private void stop() { playing = false; ((ImageView) findViewById(R.id.btnPlay)).setImageResource(R.drawable.play); Intent intent = new Intent(Player.this, PlayerService.class); intent.putExtra("option", "stop"); intent.putExtra("path", now); startService(intent); } private void seek(int duration) { Intent intent = new Intent(Player.this, PlayerService.class); intent.putExtra("option", "seek"); intent.putExtra("progress", duration); intent.putExtra("path", now); startService(intent); } private void next() { Intent intent = new Intent(Player.this, PlayerService.class); intent.putExtra("option", "next"); intent.putExtra("path", now); startService(intent); } private void prev() { Intent intent = new Intent(Player.this, PlayerService.class); intent.putExtra("option", "prev"); intent.putExtra("path", now); startService(intent); } }
package com.zy.player; import com.zy.zjg.Player; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.IBinder; import android.os.Messenger; public class PlayerService extends Service { Messenger messenger; public static MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent arg0) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { String option = intent.getStringExtra("option"); if (messenger == null) { messenger = (Messenger) intent.getExtras().get("messenger"); } if ("start".equals(option)) { start(intent.getStringExtra("path")); } else if ("pause".equals(option)) { pause(); } else if ("resume".equals(option)) { continuePlay(); } else if ("stop".equals(option)) { stop(intent.getStringExtra("path")); } else if ("seek".equals(option)) { seekPlay(intent.getIntExtra("progress", -1)); } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); } private void start(String path) { try { if (mediaPlayer == null) mediaPlayer = new MediaPlayer(); mediaPlayer.reset(); mediaPlayer.setDataSource(path); mediaPlayer.prepare(); mediaPlayer.start(); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { Player.playing = false; } }); } catch (Exception e) { e.printStackTrace(); } } private void pause() { if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.pause(); } } private void stop(String path) { if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.stop(); try { if (mediaPlayer == null) mediaPlayer = new MediaPlayer(); mediaPlayer.reset(); mediaPlayer.setDataSource(path); mediaPlayer.prepare(); } catch (Exception e) { e.printStackTrace(); } } } private void continuePlay() { if (mediaPlayer != null) { mediaPlayer.start(); } } private void seekPlay(int seek) { if (mediaPlayer != null && mediaPlayer.isPlaying()) { mediaPlayer.seekTo(seek); mediaPlayer.start(); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:orientation="vertical"> <TextView android:id="@+id/txtListTitle" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="10" android:text="列表" /> <ListView android:id="@+id/listview" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="5" android:orientation="vertical" > <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" android:orientation="horizontal" > <TextView android:id="@+id/txtTime" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="10dp" android:layout_weight="5" android:textColor="#000000" android:text="play01" /> <ImageView android:id="@+id/btnStop" style="@style/player" android:layout_weight="5" android:background="@drawable/stop" /> <ImageView android:id="@+id/btnPrev" style="@style/player" android:layout_weight="5" android:background="@drawable/prev" /> <ImageView android:id="@+id/btnPlay" style="@style/player" android:layout_weight="5" android:background="@drawable/play" /> <ImageView android:id="@+id/btnNext" style="@style/player" android:layout_weight="5" android:background="@drawable/next" /> <ImageView android:id="@+id/imgVolume" style="@style/player" android:layout_weight="5" android:background="@drawable/volume" /> <SeekBar android:id="@+id/seekbarVolume" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_weight="5" /> </LinearLayout> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/txtName" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
<style name="player"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_marginLeft">20dp</item> <item name="android:layout_marginRight">20dp</item> </style>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<service android:name="com.zy.player.PlayerService" />