Android studio简单的音乐播放器
时间: 2023-08-31 21:36:44 浏览: 69
Android Studio 简易音乐播放器
5星 · 资源好评率100%
好的,我可以为你提供一个简单的Android Studio音乐播放器的示例代码。以下是基本步骤:
1.在Android Studio中创建一个新项目。
2.将音乐文件存储在app/src/main/res/raw目录下。
3.在app/build.gradle文件中添加以下依赖项:
```java
implementation 'com.google.android.exoplayer:exoplayer:2.11.2'
```
4.在app/src/main/java/包名/MainActivity.java文件中添加以下代码:
```java
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
public class MainActivity extends AppCompatActivity {
private PlayerView mPlayerView;
private SimpleExoPlayer mExoPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlayerView = findViewById(R.id.player_view);
Button mButtonPlay = findViewById(R.id.button_play);
Button mButtonPause = findViewById(R.id.button_pause);
mButtonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initializePlayer();
}
});
mButtonPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
releasePlayer();
}
});
}
private void initializePlayer() {
mExoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
mPlayerView.setPlayer(mExoPlayer);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_music_file_name);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, getString(R.string.app_name)));
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, null, null);
mExoPlayer.prepare(mediaSource);
mExoPlayer.setPlayWhenReady(true);
}
private void releasePlayer() {
if (mExoPlayer != null) {
mExoPlayer.release();
mExoPlayer = null;
Toast.makeText(this, "MediaPlayer released", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
}
}
```
5.在app/src/main/res/layout/activity_main.xml文件中添加以下代码:
```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">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/black"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"/>
<Button
android:id="@+id/button_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"/>
</LinearLayout>
</LinearLayout>
```
这是一个简单的Android Studio音乐播放器的示例代码。你可以根据自己的需求进行更改和扩展。
阅读全文