Android Studio在多个活动中添加音乐到背景,无需重复

时间:2016-05-27 08:20:22

标签: android android-asynctask background

我试图在我的应用程序的背景中实现一个Soundfile,它将播放多个活动而不停止并重新播放它。我通过使用AsyncTask在线找到了答案,但它只是告诉我我的其他类是“未封闭”有没有人有一个建议我如何更容易或更有效地使用它?我将在这里包含代码:

public class BackgroundSound extends AsyncTask<Void, Void, Void> {

MediaPlayer mMediaPlayer;

protected void onPreExecute() {
    mMediaPlayer = MediaPlayer.create(Splashscreen.this, R.raw.mainmenu);
    //Splashscreen.this is "not an enclosing class"
}
protected Void doInBackground(Void... params) {
    mMediaPlayer.setLooping(true); // Set looping
    mMediaPlayer.setVolume(100,100);
    mMediaPlayer.start();
    return null;
}
protected void onCancelled(Void v) {
    mMediaPlayer.stop();
    mMediaPlayer.release();
}

Splashscreen代码:

public class Splashscreen extends AppCompatActivity {

private View mContentView;
BackgroundSound mBackgroundSound;

public void onAttachedToWindow() {
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

Thread splashTread;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splashscreen);
    mContentView = findViewById(R.id.lin_lay);
    hide();
    StartAnimations();
    mBackgroundSound = new BackgroundSound();

}

public void onResume() {
    super.onResume();
    mBackgroundSound.execute();

}

public void onPause() {
    super.onPause();
    mBackgroundSound.cancel(true);
}

private void StartAnimations() {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    LinearLayout l = (LinearLayout) findViewById(R.id.lin_lay);
    l.clearAnimation();
    l.startAnimation(anim);

    anim = AnimationUtils.loadAnimation(this, R.anim.translate);
    anim.reset();
    ImageView iv = (ImageView) findViewById(R.id.splash);
    iv.clearAnimation();
    iv.startAnimation(anim);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;

                while (waited < 3500) {
                    sleep(100);
                    waited += 100;
                }
                Intent intent = new Intent(Splashscreen.this,
                        MainMenu.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                Splashscreen.this.finish();
            } catch (InterruptedException e) {
            } finally {
                Splashscreen.this.finish();
            }

        }
    };
    splashTread.start();

}
private void hide() {
    mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}

提前致谢!

2 个答案:

答案 0 :(得分:0)

你绝对应该为这种交叉活动的东西使用服务。

以下指南将帮助您设置服务:https://developer.android.com/guide/components/services.html

答案 1 :(得分:0)

为工作使用服务:

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.your_sound_file);
        player.setLooping(true); 
        player.setVolume(100,100);

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

    public void onStart(Intent intent, int startId) {

    }
    public IBinder onUnBind(Intent arg0) {
        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }
}

使用Splashscreen中的onCreate()方法启动它:

Intent svc = new Intent(this, BackgroundSoundService.class);
svc.setAction("com.example.BackgroundSoundService");
startService(svc);

将BackgroundSoundService添加到清单。

<service android:enabled="true" android:name=".BackgroundSoundService" />

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.idokov.layouttests">

    <application
        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>

        <service android:enabled="true" android:name=".BackgroundSoundService" />
    </application>

</manifest>