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 th开发者_JS百科e 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
"
MediaPlayer Player2;
Player2= MediaPlayer.create(this, R.raw.music);
Player2.start();
button click event{
Player2.stop();
}
In this example i have use Mediaplayer and i have get my resource from raw folder (res/raw.music.mp3).
i have use stop();
method of media player to stop it
For more here
you should have to use finish() after statring ur activity...
edit
use mp.release();
private static final String TAG = "VideoPlayer";
private MediaPlayer mp;
private SurfaceView mPreview;
private SurfaceHolder holder;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
/**
* Called when the activity is first created.
*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// Set up the play/pause/reset/stop buttons
mPreview = (SurfaceView) findViewById(R.id.surface);
mPath = (EditText) findViewById(R.id.path);
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.pause();
}
}
});
mReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.seekTo(0);
}
}
});
mStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.stop();
mp.release();
}
}
});
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...
精彩评论