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

发布于 2024-12-07 00:09:09 字数 434 浏览 1 评论 0原文

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

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

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

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢你一世 2024-12-14 00:09:16
MediaPlayer Player2;
Player2= MediaPlayer.create(this, R.raw.music);
Player2.start();

button click event{

Player2.stop();
}

在此示例中,我使用了 Mediaplayer,并且从原始文件夹 (res/raw.music.mp3) 中获取了资源。

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

更多信息请点击此处

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

岁月流歌 2024-12-14 00:09:16

你应该在统计你的活动后使用 finish()...

编辑

use mp.release();

私有静态最终字符串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();
            }
        }
    });

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();
            }
        }
    });
疾风者 2024-12-14 00:09:16

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

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

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

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文