android studio实现音乐播放器代码
时间: 2023-06-29 07:18:39 浏览: 194
以下是一个简单的基于Android Studio的音乐播放器代码:
1. 在布局文件中添加以下代码:
```xml
<?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">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:choiceMode="singleChoice" />
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/listView"
android:layout_centerHorizontal="true"
android:text="Play" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnPlay"
android:layout_marginTop="20dp" />
</RelativeLayout>
```
2. 在MainActivity.java中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
ListView listView;
Button btnPlay;
SeekBar seekBar;
MediaPlayer mediaPlayer;
ArrayAdapter<String> adapter;
ArrayList<String> songsList;
int currentPosition = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
btnPlay = findViewById(R.id.btnPlay);
seekBar = findViewById(R.id.seekBar);
songsList = new ArrayList<>();
songsList.add("song1.mp3");
songsList.add("song2.mp3");
songsList.add("song3.mp3");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_single_choice, songsList);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
currentPosition = i;
try {
mediaPlayer.reset();
AssetFileDescriptor descriptor = getAssets().openFd(songsList.get(currentPosition));
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
mediaPlayer.prepare();
seekBar.setMax(mediaPlayer.getDuration());
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
btnPlay.setText("Play");
} else {
mediaPlayer.start();
btnPlay.setText("Pause");
}
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
btnPlay.setText("Play");
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
mediaPlayer.seekTo(i);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
```
这段代码实现了一个简单的音乐播放器,其中包括一个ListView,一个播放/暂停按钮和一个SeekBar。将音乐文件放入assets文件夹中,并将其添加到ArrayList中以进行播放。用户可以通过单击ListView中的歌曲来选择要播放的歌曲。播放/暂停按钮将开始或暂停当前选择的歌曲的播放。SeekBar将显示当前播放进度,并允许用户拖动到他们想要的位置。
阅读全文