ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

一、简介

1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity

2.PlayerActivity通过android.media.MediaPlayer实现播放,暂停、停止

二、代码
1.xml
(1)player.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent" android:layout_height="fill_parent"
 4     android:orientation="horizontal" android:paddingLeft="10dip"
 5     android:paddingRight="10dip" android:paddingTop="1dip"
 6     android:paddingBottom="1dip">
 7     <ImageButton android:id="@+id/begin" android:layout_width="wrap_content"
 8         android:layout_height="wrap_content" android:src="@drawable/begin" />
 9     <ImageButton android:id="@+id/pause" android:layout_width="wrap_content"
10         android:layout_height="wrap_content" android:src="@drawable/pause" />
11     <ImageButton android:id="@+id/stop" android:layout_width="wrap_content"
12         android:layout_height="wrap_content" android:src="@drawable/stop" />
13 </LinearLayout>

 

(2)AndroidManifest.xml注册activity

 

1 <activity android:name=".PlayerActivity" android:label="@string/app_name"/>

 

 

 

2.java
(1)LocalMp3ListActivity.java

点击歌曲时,启动PlayerActivity

1     @Override
2     protected void onListItemClick(ListView l, View v, int position, long id) {
3         Mp3Info info = infos.get(position);
4         Intent intent = new Intent();
5         intent.putExtra("mp3Info", info);
6         intent.setClass(this, PlayerActivity.class);
7         startActivity(intent);
8     }

 

(2)PlayerActivity.java

 

 1 package tony.mp3player;
 2 
 3 import java.io.File;
 4 
 5 import tony.model.Mp3Info;
 6 import android.app.Activity;
 7 import android.content.Intent;
 8 import android.media.MediaPlayer;
 9 import android.net.Uri;
10 import android.os.Bundle;
11 import android.os.Environment;
12 import android.view.View;
13 import android.view.View.OnClickListener;
14 import android.widget.ImageButton;
15 
16 public class PlayerActivity extends Activity {
17 
18     private ImageButton beginBtn = null;
19     private ImageButton pauseBtn = null;
20     private ImageButton stopBtn = null;
21     
22     private MediaPlayer mediaPlayer = null;
23     private Mp3Info info = null;
24     
25     private boolean isPlaying = false;
26     private boolean isPause = false;
27     private boolean isRelease = false;
28     
29     @Override
30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.player);
33         Intent intent = getIntent();
34         info = (Mp3Info) intent.getSerializableExtra("mp3Info");
35         beginBtn = (ImageButton) findViewById(R.id.begin);
36         pauseBtn = (ImageButton) findViewById(R.id.pause);
37         stopBtn = (ImageButton) findViewById(R.id.stop);
38         
39         beginBtn.setOnClickListener(new BeginListener());
40         pauseBtn.setOnClickListener(new PauseListener());
41         stopBtn.setOnClickListener(new StopListener());
42     }
43     
44     class BeginListener implements OnClickListener {
45         @Override
46         public void onClick(View v) {
47             if(!isPlaying) {
48                 String path = getMp3Path(info.getMp3Name());
49                 mediaPlayer = MediaPlayer.create(PlayerActivity.this, Uri.parse("file://" + path));
50                 mediaPlayer.start();
51                 isPlaying = true;
52                 isRelease = false;
53             }
54         }
55     }
56     
57     class PauseListener implements OnClickListener {
58         @Override
59         public void onClick(View v) {
60             if(mediaPlayer != null) {
61                 if(!isRelease){
62                     if(!isPause) {
63                         mediaPlayer.pause();
64                         isPause = true;
65                     } else {
66                         mediaPlayer.start();
67                         isPause = false;
68                     }
69                 }
70             }
71         }
72     }
73     
74     class StopListener implements OnClickListener {
75         @Override
76         public void onClick(View v) {
77             if(mediaPlayer != null) {
78                 if(isPlaying) {
79                     if(!isRelease) {
80                         mediaPlayer.stop();
81                         mediaPlayer.release();
82                         isRelease = true;
83                         isPlaying = false;
84                     }
85                 }
86             }
87         }
88     }
89     
90     private String getMp3Path(String mp3Name) {
91         String sdCartRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
92         String path = sdCartRoot + File.separator + "mp3" + File.separator + mp3Name;
93         return path;
94     }
95 }

 

 

 

 

你可能感兴趣的:(ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3)