Android开发网
Android简易版音乐播放器
1. 布局
XML/HTML代码
- <?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"
- android:gravity="center_horizontal">
-
- <Button
- android:id="@+id/musicList"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:background="#86B2F4"
- android:text="音乐文件列表"
- android:textColor="#fff"
- android:textSize="28sp"/>
-
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/logo"/>
-
- <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:gravity="center_horizontal">
-
- <ImageButton
- android:id="@+id/stop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/stop"/>
-
- <ImageButton
- android:id="@+id/pre"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/pree"/>
-
- <ImageButton
- android:id="@+id/play"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/play"/>
-
- <ImageButton
- android:id="@+id/next"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/next"/>
- </LinearLayout>
- </LinearLayout>
2. 工具类
Java代码
-
-
-
-
-
- public class MusicFileNameFilter implements FilenameFilter {
-
- private String type;
-
- public MusicFileNameFilter(String type) {
- this.type = type;
- }
-
- @Override
- public boolean accept(File dir, String filename) {
- return filename.endsWith(type);
- }
- }
3. Binder接口
Java代码
-
-
-
- public interface IMusicPlayerService {
- public void callplay(String path);
- ;
- public void callStop();
-
- public boolean callIsPlaying();
-
- public int callGetgetDuration();
-
- public int callGetgetCurrentDuration();
-
- public boolean callMediaIsNull();
-
- public void callChanageSeek(int position);
-
- public void callPause();
- }
4. Service
5. Activity
Java代码
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
-
- private static final String PATH = Environment.getExternalStorageDirectory() + "/Music/";
- private static final String TAG = "MainActivity";
-
- private ArrayList<String> fileList = new ArrayList<String>();
-
- private ArrayList<String> fileNameList = new ArrayList<String>();
-
-
-
-
- private SeekBar seekBar;
-
-
-
- private Button musicList;
-
-
-
-
- private ImageButton stop;
- private ImageButton pre;
- private ImageButton play;
- private ImageButton next;
-
- private String musicPath;
-
- private IMusicPlayerService mPlayerService;
-
- private MusicPlayerServiceConnection mConn;
-
- private boolean mBound =false;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
-
- seekBar = (SeekBar) findViewById(R.id.seekBar);
-
- musicList = (Button) findViewById(R.id.musicList);
-
- stop = (ImageButton) findViewById(R.id.stop);
- pre = (ImageButton) findViewById(R.id.pre);
- ;
- play = (ImageButton) findViewById(R.id.play);
- ;
- next = (ImageButton) findViewById(R.id.next);
- ;
-
-
- File file = new File(PATH);
-
-
- File[] arrs = file.listFiles(new MusicFileNameFilter(".mp3"));
-
- for (File f : arrs) {
-
- fileList.add(f.getAbsolutePath());
-
- fileNameList.add(f.getName());
-
- }
-
-
-
-
- musicList.setOnClickListener(this);
- stop.setOnClickListener(this);
- pre.setOnClickListener(this);
- play.setOnClickListener(this);
- next.setOnClickListener(this);
-
-
-
- seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
-
- }
-
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
-
- }
-
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- mPlayerService.callChanageSeek(seekBar.getProgress());
- }
- });
-
- }
-
- @Override
- protected void onStart() {
- super.onStart();
- if(mConn==null){
- mConn= new MusicPlayerServiceConnection();
- }
- Intent intent = new Intent(this,MusicPlayerService.class);
- mBound = bindService(intent, mConn, BIND_AUTO_CREATE);
-
- }
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.musicList:
- showList();
- break;
- case R.id.stop:
- stop();
- break;
- case R.id.pre:
- pre();
- break;
- case R.id.play:
- play();
- break;
- case R.id.next:
- next();
- break;
- }
-
- }
-
-
-
- private void next() {
- int index = fileList.indexOf(musicPath);
- if(index>=fileList.size()){
- index=0;
- }
- mPlayerService.callplay(fileList.get(index+1));
- }
-
-
-
-
- private void play() {
- Log.d(TAG, "mBound" + mBound);
- boolean isNull = mPlayerService.callMediaIsNull();
- if(isNull) {
- mPlayerService.callplay(musicPath);
- }else{
- mPlayerService.callPause();
- }
-
- if(mPlayerService.callIsPlaying()){
- play.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.pause));
- }else{
- play.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.play));
- }
-
-
- new Thread(){
-
- boolean isFinished=mPlayerService.callIsPlaying();
- @Override
- public void run() {
- if(isFinished) {
- while (isFinished) {
- SystemClock.sleep(200);
- int currentDuration = mPlayerService.callGetgetCurrentDuration();
- int duration = mPlayerService.callGetgetDuration();
- seekBar.setMax(duration);
- seekBar.setProgress(currentDuration);
- if (currentDuration >= duration) {
- isFinished = false;
- }
- }
- }
-
- }
- }.start();
- }
-
-
-
-
- private void pre() {
- int index = fileList.indexOf(musicPath);
- if(index<=0){
- index=fileList.size()-1;
- }
- mPlayerService.callplay(fileList.get(index-1));
- }
-
-
-
-
- private void stop() {
- mPlayerService.callStop();
- }
-
-
-
-
- private void showList() {
-
- Intent intent = new Intent(this,MusicListActivity.class);
-
- intent.putStringArrayListExtra("filenamelist",fileNameList);
-
- startActivityForResult(intent,100);
-
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
-
- if(data==null){
- Toast.makeText(MainActivity.this, "没有结果", Toast.LENGTH_SHORT).show();
- return;
- }
-
- int position = data.getIntExtra("position", 0);
-
- musicPath = fileList.get(position);
-
- Log.d(TAG,musicPath);
- }
-
- private class MusicPlayerServiceConnection implements ServiceConnection{
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- mPlayerService = (IMusicPlayerService) service;
-
-
- }
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- if(mConn!=null){
- mConn =null;
- }
- }
- }
-
- @Override
- protected void onStop() {
- super.onStop();
- if(mConn!=null){
- unbindService(mConn);
- mConn=null;
- mPlayerService=null;
- }
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if(mConn!=null){
- unbindService(mConn);
- mConn=null;
- mPlayerService=null;
- }
- }
- }
Tags:音频 | 2016/3/21 | 发表评论