-1

I am developing a simplle Android Application to play an .mp3 file from device storage by giving the path. I need to play that mp3 file from Background and show a notification by song title...

How i can use a Service and play music in bakground by displaying song title..

The code I used is ...

Mainactivity.java

public class MainActivity extends Activity {
    Button start,pause,stop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start=(Button)findViewById(R.id.button1);
        pause=(Button)findViewById(R.id.button2);
        stop=(Button)findViewById(R.id.button3);
        //creating media player
        final MediaPlayer mp=new MediaPlayer();
        try{
            //you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3
            mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Jithin's/downloadedfile.mp3");

            mp.prepare();
        }catch(Exception e){e.printStackTrace();}

        start.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
            }
        });
        pause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.pause();
            }
        });
        stop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
            }
        });
    }
}

Can anyone help me to find the code .. ThankYou ....

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
Jithin Ezio
  • 171
  • 12

4 Answers4

0

You can look at this to get the idea.

Aneeb Khawar
  • 330
  • 1
  • 8
0

Try to look into this or this

And make sure to register service in your manifest file

Community
  • 1
  • 1
jettimadhuChowdary
  • 1,058
  • 1
  • 13
  • 23
0

Did you create a service for running your activity in background?

Maybe this could help you:

How to keep activity running in background?

Community
  • 1
  • 1
Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60
0

Try this

   try {
                mp = new MediaPlayer();

               mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Jithin's/downloadedfile.mp3");
                mp.setLooping(false); // Set looping
                mp.setVolume(100,100);
                mp.prepare();
                mp.start();
            } catch (IOException e) {
                e.printStackTrace();
            }





@Override
protected void onResume() {

        try {

                mp.prepare();
                mp.start();

        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    super.onResume();
}


@Override
protected void onDestroy() {
    super.onDestroy();

            mp.stop();

    }
}

Note: You sure the Path exist... so when you start music or stop check path is exist than play

Arjun saini
  • 4,223
  • 3
  • 23
  • 51