Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Background Sound in android applications

Tags:

android

audio

I want to play background sound in my app which I made. Help me how can I do this?...Here is the entire code.

public class Numbers extends Activity {
    public static MediaPlayer mp = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);
        ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);
    }

    private class ImagePagerAdapter extends PagerAdapter {
        private int[] mImages = new int[]{R.drawable.no1,R.drawable.no2,R.drawable.no3,R.drawable.no4,R.drawable.no5,R.drawable.no6,R.drawable.no7,R.drawable.no8,R.drawable.no9};

        @Override
        public int getCount() {
            return mImages.length;
        }
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = Numbers.this;
            ImageView imageView = new ImageView(context);
            int padding =context.getResources().  
            getDimensionPixelSize(R.dimen.activity_vertical_margin);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ((ViewPager) container).removeView((ImageView) object);
            }
        }
    }//end of sub-class ImagePagerAdapter
}//End of Numbers class

Just tell me what I need to add in this code to play background music which will be in loop mode till the app runs.

like image 630
SuRaj Creator Avatar asked Jan 10 '14 11:01

SuRaj Creator


People also ask

How do you play background noise?

Turn on Background SoundsOpen Control Center and tap the Hearing Devices button . If you don't see the Hearing Devices button, learn how to add it to Control Center. Tap Background Sounds to turn it on. Tap the name of the current sound to view other sounds, then tap a sound name to listen to a preview.

How do I play background music in Android?

This example demonstrates how do I play background music in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

How do I enable background audio on my Windows Phone?

Under that Tap on the check box for Background play ensure that “Background play (audio)” is enabled (i.e. its check box is selected) Just like Android users, currently Windows Phone users are also dependent on apps for this purpose. For Windows Phone users I would recommend them to use VLC beta app version 1.0.7 from the store.

How to run YouTube videos in the background on Android?

Open the YouTube app on your Android or iOS device and log in with the account which holds premium membership. Play any video that you like and just hit the home button or swipe up from the bottom, the video will be played in the background. And that is how you can run YouTube videos in the background with the official YouTube app itself.

What is the best way to play media in background?

Better to put your media code in service. It is best way to play media in background. where raw is folder created in resources. and R.raw.b is an mp3 file. Show activity on this post.


2 Answers

This is tested in android studio 2.2.3

1) first copy and paste your music.mp3 into app.res.raw.

2) set service into AndroidManifest.xml be like this:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    :
    :
    <service android:name=".SoundService"  android:enabled="true"></service>
</application>

3) Add SoundService.java file with contain this code:

package com.jahanweb.ring;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class SoundService extends Service {
    MediaPlayer player;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onCreate() {
        player = MediaPlayer.create(this, R.raw.music); //select music file
        player.setLooping(true); //set looping
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return Service.START_NOT_STICKY;
    }

    public void onDestroy() {
        player.stop();
        player.release();
        stopSelf();
        super.onDestroy();
    }

}

4) use it in the activity be like this:

package com.jahanweb.ring;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //start service and play music
        startService(new Intent(MainActivity.this, SoundService.class));

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    protected void onDestroy() {
        //stop service and stop music
        stopService(new Intent(MainActivity.this, SoundService.class));
        super.onDestroy();
    }
}
like image 112
Nabi K.A.Z. Avatar answered Sep 27 '22 17:09

Nabi K.A.Z.


Better to put your media code in service. It is best way to play media in background.

public class serv extends Service{

    MediaPlayer mp;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    public void onCreate()
    {   
        mp = MediaPlayer.create(this, R.raw.b);
        mp.setLooping(false);
    }
    public void onDestroy()
    {       
        mp.stop();
    }
    public void onStart(Intent intent,int startid){

        Log.d(tag, "On start");
        mp.start();
    }
}

where raw is folder created in resources. and R.raw.b is an mp3 file.

like image 41
Ravi Avatar answered Sep 27 '22 17:09

Ravi