やること
- 暗黙的 Intent を飛ばして音楽ファイルの Uri を取ってくる。
- Uri を元に音楽を MediaPlayer で再生する。
プログラム
レイアウト
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is music picker test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.093" />
<Button
android:id="@+id/button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:layout_marginEnd="8dp"
android:text="register"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="8dp"
android:text="play"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_register" />
</android.support.constraint.ConstraintLayout>
Activity
MainActivity.java
public class MainActivity extends AppCompatActivity {
final int MUSIC_REQUEST = 100;
Button buttonPlay;
Button buttonRegister;
Uri audioUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
buttonPlay = findViewById(R.id.button_play);
buttonRegister = findViewById(R.id.button_register);
// Set Listener
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
play();
}
});
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
register();
}
});
}
private void play() {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(getApplicationContext(), audioUri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
private void register() {
Intent pickAudioIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickAudioIntent, MUSIC_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == MUSIC_REQUEST && resultCode == RESULT_OK) {
audioUri = intent.getData();
}
}
}