在 android 应用程序中播放背景声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21043059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play Background Sound in android applications
提问by SuRaj Creator
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.
只需告诉我需要在此代码中添加什么来播放背景音乐,该背景音乐将处于循环模式,直到应用程序运行。
回答by Ravi
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.
其中 raw 是在资源中创建的文件夹。并且R.raw.b
是一个mp3文件。
回答by Nabi K.A.Z.
This is tested in android studio 2.2.3
这是在android studio 2.2.3中测试的
1) first copy and paste your music.mp3
into app.res.raw
.
1)首先复制并粘贴music.mp3
到app.res.raw
.
2) set service into AndroidManifest.xml
be like this:
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) Add SoundService.java
file with contain this code:
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) use it in the activity be like this:
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();
}
}
回答by priyanka morisetti
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
}
回答by Riskhan
Try below link... hope this will work
试试下面的链接...希望这会奏效