Friday, 3 January 2014

Play the Media Sound! (Background music)

Can you Imagine a life without music? Please don't imagine 😊

This tutorial is all about implementing background music in your apps and especially games.


Keep the music.mp3 file in main->res->raw folder.
The guitar or any image you wish should be kept in the drawable folder.
I downloaded the image from Pixabay.

------------------------- play_media_activity.xml --------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff9e22"
android:weightSum="10">

<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="9"
android:src="@drawable/guitar"/>

<Button
android:text="Start Background Music"
android:id="@+id/pma_play_music_BTN"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#EE6002"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_gravity="center"/>

</LinearLayout>
-----------------------------------------------------------------

----------------------The java code---------------
package com.app.yourappname;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class PlayMediaActivity extends AppCompatActivity {

private MediaPlayer mp = null;
private Button playBTN;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.play_media_activity);
initMedia();
playBTN = findViewById(R.id.pma_play_music_BTN);
playBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(mp!=null && !mp.isPlaying()) {
mp.start();
playBTN.setText("Background Music Started!");
}
else {
mp.pause();
mp.seekTo(0);
playBTN.setText("Background Music is not Started");
}
}
});
}

private void initMedia() {
try{
mp = new MediaPlayer();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp)
{}
});

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{ mp.reset(); }
});
mp = MediaPlayer.create(this, R.raw.music);
//mp.setVolume(100.0f, 1000.0f);
// mp.prepareAsync();
} catch(Throwable t) {
android.util.Log.e("PlayMediaActivity", "Exception in playing audio", t);
Toast.makeText(this, "Media Player Exception...", Toast.LENGTH_LONG).show();
}
}

@Override
protected void onResume() {
super.onResume();
if(mp!=null) {
if(mp.isPlaying()) {
playBTN.setText("Background Music Started!");
}
else {
playBTN.setText("Background Music is not Started");
}
}
}

@Override
public void onPause() {
super.onPause();
if(mp!=null) {
mp.pause();
mp.seekTo(0);
}
}
}
--------------------------------------------------------

------------------------------------ Declare Activity name in Manifest file ------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".PlayMediaActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------------------------------------

No comments:

Post a Comment