Search code examples
androidmedia-player

How to close the default media player?


I have started the default media player using the following code:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(Environment.getExternalStorageDirectory()+("/background.mp3"));
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent); 

But, when the file is played completely, I want to exit the media player. How do we close the default media player which is started by 'intent'? I have tried using the "startActivityForResult()"... But i don't know where to set the "result"


Solution

  • Thanks all for your reply, I got the problem... I had used:

    File file = new File(Environment.getExternalStorageDirectory()+("/background.mp3"));
    

    in my code. The Environment.getExternalStorageDirectory() was the problem!!! When i replaced it with "/sdcard/background.mp3" it works properly...