1

I know people made posts about this but I am still confused on how to apply It for my app. here is my code Hopefully you guys can solve this

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 Code:

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

}

Once again thank you for the help! I'm new to this.

  • 2
    You need to tell us what's wrong with it. We're not going to try and run it to see. – weston Jan 01 '17 at 13:55
  • Music does not play and there is some red error shown on the screenshots. for MainActivity : http://prntscr.com/dqantr and for LynxMusic: http://prntscr.com/dqanya Also thank you for editing and correcting my post! –  Jan 01 '17 at 13:58
  • Oh, you can't put code in the body of a class like that. `startActivity(myIntent);` needs to go inside a method otherwise when do you suppose that'll get run? – weston Jan 01 '17 at 14:02
  • And `myIntent` is not in scope here `MediaPlayer.create(myIntent.this, R.raw.lynx);` And it doesn't play music because it doesn't compile. – weston Jan 01 '17 at 14:03
  • What do I do to make It compile? –  Jan 01 '17 at 14:11
  • "I know people made posts about this but I am still confused on how to apply It for my app" show us the other questions, because I can't see what you are trying for here. – weston Jan 01 '17 at 14:15
  • http://stackoverflow.com/questions/7928803/background-music-android/7928911#7928911 Official post, I want It to loop the lynx.mp3 from raw file when every a user opens the app it auto starts playing the .mp3 file in a loop. –  Jan 01 '17 at 14:25
  • There's no mention of `intent` or `startActivity` there is there? Delete those lines and re-read that post. – weston Jan 01 '17 at 14:27
  • `startActivity` is for starting `Activity`s not starting a background task. – weston Jan 01 '17 at 14:30
  • I could try. thank you tho –  Jan 01 '17 at 14:37
  • One key point you might have missed is "Enclosed in your activity class" LynxMusic needs to be a subclass of the activity. – weston Jan 01 '17 at 14:39
  • Thank you :) will try out the code right now! –  Jan 01 '17 at 14:41
  • I already have subclass For MainActivity that is BaseThemedActivity how would I add a subclass for the music aswell? –  Jan 01 '17 at 14:52
  • sorry, not subclass, I meant nested class. – weston Jan 01 '17 at 15:06
  • If you need to music to play for all activities, you should start a service when application starts to play music in background. – Kartik Sharma Jan 01 '17 at 15:08

1 Answers1

1

In order for media to be played in the background of your app location when the user is interacting with it you must start a Service from your application's main activity and the service shall contain all the methods related to playback. To allow activity to interact with the service, a service connection is also required. In short, we need to implement a bounded service.

Refer

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

SamHoque
  • 2,978
  • 2
  • 13
  • 43