在Android应用程序中播放背景声音

我想在我的应用程序中播放背景音乐。 帮帮我,我该怎么做?…这是整个代码。

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 

只要告诉我,我需要添加这个代码来播放背景音乐,这将是循环模式,直到应用程序运行。

Solutions Collecting From Web of "在Android应用程序中播放背景声音"

更好地把你的媒体代码服务。 这是在后台播放媒体的最佳方式。

 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(); } } 

原始文件夹是在资源中创build的。 R.raw.b是一个mp3文件。

 MediaPlayer player = MediaPlayer.create(this, R.raw.music); player.setLooping(true); // Set looping player.setVolume(100,100); public int onStartCommand(Intent intent, int flags, int startId) { player.start(); return 1; } @Override public void onDestroy() { player.stop(); player.release(); } public void onStart(Intent intent, int startId) { // TODO } 

尝试下面的链接…希望这将工作

如何在android中使用android服务播放audio

这在android studio 2.2.3中testing

1)首先复制并粘贴到app.res.raw你的app.res.raw

2)将服务设置为AndroidManifest.xml如下所示:

 <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)添加包含以下代码的SoundService.java文件:

 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)在活动中使用它是这样的:

 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(); } }