Hello friends,
If you want play background music without using service then use this sample code
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tech.samset.MainActivity">
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
MainActvity.java
package com.tech.samset;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActvity.class);
startActvity(intent );
}
});
}
}
package com.tech.samset;
import android.content.Context;
import android.media.MediaPlayer;
import android.widget.Toast;
import com.example.user.audiomusic.R;
/**
* Created by user on 02-Jul-16.
*/
public class MyMusic implements MediaPlayer.OnErrorListener {
MediaPlayer mPlayer;
private int length = 0;
public MyMusic(Context c)
{
mPlayer = MediaPlayer.create(c, R.raw.background_sound);
mPlayer.setOnErrorListener(this);
if (mPlayer != null) {
mPlayer.setLooping(true);
mPlayer.setVolume(100, 100);
}
mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
onError(mPlayer, what, extra);
return true;
}
});
}
public void pauseMusic() {
if (mPlayer!=null)
{
if (mPlayer.isPlaying()) {
mPlayer.pause();
length = mPlayer.getCurrentPosition();
}
}
}
public void resumeMusic() {
if (mPlayer!=null)
{
if (mPlayer.isPlaying() == false) {
mPlayer.seekTo(length);
mPlayer.start();
}
}
}
public void stopMusic() {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
public void onDestroy() {
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// Toast.makeText(c, "music player failed", Toast.LENGTH_SHORT).show();
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
return false;
}
}
MyApplication.java
public class MyApplication extends Application {
public MyMusic myMusic;
@Override
public void onCreate() {
super.onCreate();
myMusic=new MyMusic(this);
}
public void onMusicPause()
{
myMusic.pauseMusic();
}
public void onMusicResume()
{
myMusic.resumeMusic();
}
public void onMusicDestroy()
{
myMusic.onDestroy();
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tech.samset">
<application
android:name=.MyApplication
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActvity" />
</application>
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.user.audiomusic"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.sjl:Foredroid:1.0.0'
}
If you want play background music without using service then use this sample code
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tech.samset.MainActivity">
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
MainActvity.java
package com.tech.samset;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActvity.class);
startActvity(intent );
}
});
}
}
SecondActvity.java
public class SecondActvity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Mymusic.javapublic class SecondActvity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
package com.tech.samset;
import android.content.Context;
import android.media.MediaPlayer;
import android.widget.Toast;
import com.example.user.audiomusic.R;
/**
* Created by user on 02-Jul-16.
*/
public class MyMusic implements MediaPlayer.OnErrorListener {
MediaPlayer mPlayer;
private int length = 0;
public MyMusic(Context c)
{
mPlayer = MediaPlayer.create(c, R.raw.background_sound);
mPlayer.setOnErrorListener(this);
if (mPlayer != null) {
mPlayer.setLooping(true);
mPlayer.setVolume(100, 100);
}
mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
onError(mPlayer, what, extra);
return true;
}
});
}
public void pauseMusic() {
if (mPlayer!=null)
{
if (mPlayer.isPlaying()) {
mPlayer.pause();
length = mPlayer.getCurrentPosition();
}
}
}
public void resumeMusic() {
if (mPlayer!=null)
{
if (mPlayer.isPlaying() == false) {
mPlayer.seekTo(length);
mPlayer.start();
}
}
}
public void stopMusic() {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
public void onDestroy() {
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// Toast.makeText(c, "music player failed", Toast.LENGTH_SHORT).show();
if (mPlayer != null) {
try {
mPlayer.stop();
mPlayer.release();
} finally {
mPlayer = null;
}
}
return false;
}
}
BaseActvity.java
package com.example.user.audiomusic.parentActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.example.user.audiomusic.MyApplication;
import com.example.user.audiomusic.R;
import com.sjl.foreground.Foreground;
public abstract class BaseActivity extends AppCompatActivity implements Foreground.Listener {
private Foreground.Binding listenerBinding;
//protected abstract void startAnotherActivity();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listenerBinding = Foreground.get(getApplication()).addListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
listenerBinding.unbind();
Log.i("Base", " onDestroy");
}
@Override
public void onBecameForeground() {
MyApplication.getInstance().onMusicResume();
Toast.makeText(BaseActivity.this,"App is foreground",Toast.LENGTH_SHORT).show();
Log.i(Foreground.TAG, getClass().getName() + " became foreground");
}
@Override
public void onBecameBackground() {
MyApplication.getInstance().onMusicPause();
Toast.makeText(BaseActivity.this,"App went background",Toast.LENGTH_SHORT).show();
Log.i(Foreground.TAG, getClass().getName() + " went background");
}
}
public class MyApplication extends Application {
public MyMusic myMusic;
@Override
public void onCreate() {
super.onCreate();
myMusic=new MyMusic(this);
}
public void onMusicPause()
{
myMusic.pauseMusic();
}
public void onMusicResume()
{
myMusic.resumeMusic();
}
public void onMusicDestroy()
{
myMusic.onDestroy();
}
return mInstance;
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tech.samset">
<application
android:name=.MyApplication
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActvity" />
</application>
</manifest>
build.gradle
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.user.audiomusic"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.sjl:Foredroid:1.0.0'
}
Thank you
No comments:
Post a Comment