Play ▶ Some audio files inside your application.
Using XML and Java code in Android Studio.
At first UI design Using XML.
Step1: Simple design
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/PlayOffline"
android:layout_width="300dp"
android:layout_height="80dp"
android:backgroundTint="#0043A6"
android:textSize="25dp"
android:text="Offline audio Play"/>
</LinearLayout>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/PlayOffline"
android:layout_width="300dp"
android:layout_height="80dp"
android:backgroundTint="#0043A6"
android:textSize="25dp"
android:text="Offline audio Play"/>
</LinearLayout>
Step2: Insert your audio file in the Android studio
- Inspect the res file
- Right click
- Select new
- Create a new Directory
- Directory name is raw
- Past your audio in the raw folder
public class MainActivity extends AppCompatActivity {
Button PlayOffline;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlayOffline = findViewById(R.id.PlayOffline);
PlayOffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.offline_audio);
mediaPlayer.start();
}
});
}
}
Button PlayOffline;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlayOffline = findViewById(R.id.PlayOffline);
PlayOffline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.offline_audio);
mediaPlayer.start();
}
});
}
}
Now Complete Your Offline audio player
😊
😊
😊
Now We are playing an online audio
PlayOnline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("https://file-examples.com/file_example_MP3_700KB.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
}
});
@Override
public void onClick(View v) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("https://file-examples.com/file_example_MP3_700KB.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
}
});
try/catch your code and done
PlayOnline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("https://file-examples.com/file_example_MP3_700KB.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
@Override
public void onClick(View v) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("https://file-examples.com/file_example_MP3_700KB.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
If you want to play through multiple audio media players then you have to give this condition to each button:-
if (mediaPlayer!=null) mediaPlayer.release();
PlayOnline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer!=null) mediaPlayer.release();
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("https://file-examples.com/file_example_MP3_700KB.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
@Override
public void onClick(View v) {
if (mediaPlayer!=null) mediaPlayer.release();
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("https://file-examples.com/file_example_MP3_700KB.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});



Post a Comment