如何关闭默认媒体播放器?

时间:2011-09-29 05:54:06

标签: android media-player

我使用以下代码启动了默认媒体播放器:

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

但是,当文件完全播放时,我想退出媒体播放器。 我们如何关闭由'intent'启动的默认媒体播放器? 我尝试过使用“startActivityForResult()”...但我不知道在哪里设置“result

3 个答案:

答案 0 :(得分:0)

你应该在开始活动后使用finish()......

修改

使用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();
            }
        }
    });

答案 1 :(得分:0)

MediaPlayer Player2;
Player2= MediaPlayer.create(this, R.raw.music);
Player2.start();

button click event{

Player2.stop();
}

在这个例子中我使用了Mediaplayer,我从raw文件夹(res / raw.music.mp3)获取了我的资源。

我使用媒体播放器的stop();方法来阻止它

For more here

答案 2 :(得分:0)

感谢大家的回复,我遇到了问题...... 我用过:

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

在我的代码中。问题是Environment.getExternalStorageDirectory()! 当我用“/sdcard/background.mp3”替换它时,它可以正常工作......