用android写一个简单的音乐播放器
时间: 2023-10-05 12:07:36 浏览: 78
好的,下面是一个简单的 Android 音乐播放器的示例代码:
1. 首先,在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
```
2. 创建布局文件 activity_main.xml,包含一个 ListView 和一个 Button:
```xml
<?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" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/buttonPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
</LinearLayout>
```
3. 创建自定义适配器 MusicListAdapter.java,用于在 ListView 中显示音乐列表:
```java
public class MusicListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Music> mMusicList;
public MusicListAdapter(Context context, ArrayList<Music> musicList) {
mContext = context;
mMusicList = musicList;
}
@Override
public int getCount() {
return mMusicList.size();
}
@Override
public Object getItem(int position) {
return mMusicList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, parent, false);
}
TextView textView = convertView.findViewById(android.R.id.text1);
textView.setText(mMusicList.get(position).getTitle());
return convertView;
}
}
```
4. 创建实体类 Music.java,表示一首歌曲:
```java
public class Music {
private String mTitle;
private String mPath;
public Music(String title, String path) {
mTitle = title;
mPath = path;
}
public String getTitle() {
return mTitle;
}
public String getPath() {
return mPath;
}
}
```
5. 在 MainActivity.java 中实现音乐播放器的逻辑:
```java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ListView mListView;
private Button mButtonPlay;
private ArrayList<Music> mMusicList;
private MediaPlayer mMediaPlayer;
private int mCurrentPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.listView);
mButtonPlay = findViewById(R.id.buttonPlay);
mButtonPlay.setOnClickListener(this);
mMusicList = new ArrayList<>();
loadMusicList();
MusicListAdapter adapter = new MusicListAdapter(this, mMusicList);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCurrentPosition = position;
playMusic(mMusicList.get(position).getPath());
}
});
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.buttonPlay) {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
mButtonPlay.setText("Play");
} else {
if (mMediaPlayer != null) {
mMediaPlayer.start();
mButtonPlay.setText("Pause");
} else {
if (mMusicList.size() > 0) {
mCurrentPosition = 0;
playMusic(mMusicList.get(0).getPath());
}
}
}
}
}
private void loadMusicList() {
String[] projection = {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA};
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
mMusicList.add(new Music(title, path));
} while (cursor.moveToNext());
cursor.close();
}
}
private void playMusic(String path) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
mButtonPlay.setText("Pause");
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mCurrentPosition++;
if (mCurrentPosition >= mMusicList.size()) {
mCurrentPosition = 0;
}
playMusic(mMusicList.get(mCurrentPosition).getPath());
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}
```
6. 运行应用程序,点击列表中的歌曲即可播放,点击播放按钮可以切换播放和暂停状态。
阅读全文