如何在所有活动的背景中播放音乐?

时间:2017-01-01 13:35:46

标签: java android

我知道人们对此发表了帖子,但我仍然对如何将它应用于我的应用感到困惑。这是我的代码希望你们能解决这个问题

MainActivity Code:

package an.lynxstore;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.net.Uri;

import an.lynxstore.a.ATE;
import an.lynxstore.base.BaseThemedActivity;
import an.lynxstore.dialogs.AboutDialog;

public class MainActivity extends BaseThemedActivity implements NavigationView.OnNavigationItemSelectedListener {

Intent myIntent = new Intent(MainActivity.this, LynxMusic.class);
startActivity(myIntent);

private DrawerLayout mDrawer;

@SuppressWarnings("ConstantConditions")
@Override
protected void onCreate(Bundle savedInstanceState) {

    if (!ATE.config(this, "light_theme").isConfigured(4)) {
        ATE.config(this, "light_theme")
                .activityTheme(R.style.AppTheme)
                .primaryColorRes(R.color.colorPrimaryLightDefault)
                .accentColorRes(R.color.colorAccentLightDefault)
                .coloredNavigationBar(false)
                .navigationViewSelectedIconRes(R.color.colorAccentLightDefault)
                .navigationViewSelectedTextRes(R.color.colorAccentLightDefault)
                .commit();
    }
    if (!ATE.config(this, "dark_theme").isConfigured(4)) {
        ATE.config(this, "dark_theme")
                .activityTheme(R.style.AppThemeDark)
                .primaryColorRes(R.color.colorPrimaryDarkDefault)
                .accentColorRes(R.color.colorAccentDarkDefault)
                .coloredNavigationBar(true)
                .navigationViewSelectedIconRes(R.color.colorAccentDarkDefault)
                .navigationViewSelectedTextRes(R.color.colorAccentDarkDefault)
                .commit();
    }

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

    final Toolbar toolbar = (Toolbar) findViewById(R.id.appbar_toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle(R.string.app_name);
    toolbar.setNavigationIcon(R.drawable.ic_menu);

    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawer.setDrawerListener(new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close));

    final NavigationView navView = (NavigationView) findViewById(R.id.navigation_view);
    navView.setNavigationItemSelectedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);


    return super.onCreateOptionsMenu(menu);
}



@Override
public boolean onNavigationItemSelected(MenuItem item) {
    mDrawer.closeDrawers();
    final int mItemId = item.getItemId();
    mDrawer.postDelayed(new Runnable() {
        @Override
        public void run() {
            switch (mItemId) {
                case R.id.drawer_settings:
                    startActivity(new Intent(MainActivity.this, SettingsActivity.class));
                    break;
                case R.id.drawer_about:
                    AboutDialog.show(MainActivity.this);
                    break;
            }
        }
    }, 75);
    return true;
}

LynxMusic代码:

package an.lynxstore;

import android.media.MediaPlayer;
import android.os.AsyncTask;

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

@Override
protected Void doInBackground(Void... params) {
    MediaPlayer player = MediaPlayer.create(myIntent.this, R.raw.lynx);
    player.setLooping(true); // Set looping
    player.setVolume(100,100);
    player.start();

    return null;
}

}

再次感谢您的帮助!我是新手。

1 个答案:

答案 0 :(得分:1)

为了在用户与其进行交互时在应用程序位置的后台播放媒体,您必须从应用程序的主要活动启动服务,该服务应包含与播放相关的所有方法。要允许活动与服务交互,还需要服务连接。简而言之,我们需要实现有界服务。

参见

  

http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App