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.
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.
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
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.
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.
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.
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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With