Android 媒体播放器

发布于 2024-12-05 01:47:48 字数 752 浏览 0 评论 0原文

我想在此活动中准备我的媒体播放器,准备好后我想跳转到另一个活动并在那里启动媒体播放器。它没有跳到其他活动,你能帮我吗?

        public void run() {

            try {
                mp.setDataSource(urls.getFirst());
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);       
                mp.setOnPreparedListener(new OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        Intent i = new Intent(Start.this, RadyoBabylonActivity.class);
                        startActivity(i);
                    }
                });
                mp.prepare();   

            } catch (Exception e) {

                e.printStackTrace();
            }   
        }
    });
    th.start();

I want to prepare my mediaplayer on this activity and when its prepared I want to jump to another activity and start the mediaplayer there. It doesn't jump to the other activity can you help me please?

        public void run() {

            try {
                mp.setDataSource(urls.getFirst());
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);       
                mp.setOnPreparedListener(new OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        Intent i = new Intent(Start.this, RadyoBabylonActivity.class);
                        startActivity(i);
                    }
                });
                mp.prepare();   

            } catch (Exception e) {

                e.printStackTrace();
            }   
        }
    });
    th.start();

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

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

发布评论

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

评论(1

深海里的那抹蓝 2024-12-12 01:47:48

我相信这里的问题是您试图直接从后台线程启动 Activity 。我不认为这可以直接在 Android 中完成 - 相反,您必须让 Activity 从 UI 线程启动。因此,在Android中执行此操作的方法是使用HandlerHandler 存在于 UI 线程中,并从后台线程接收消息来执行 UI 操作,例如启动另一个 Activity

示例:

//Inside your activity:
final Handler messageHandler = new Handler() {
    public void handleMessage(Message msg) 
    {
        //Start Activity
        Intent i = new Intent(Start.this, RadyoBabylonActivity.class);
        startActivity(i);
    }
}

//Inside onPrepared(MediaPlayer mp):
messageHandler.sendEmptyMessage(0);

另请参阅: http://developer.android.com/reference/android/ os/Handler.html

I believe the issue here is that you are trying to start an Activity directly from a background thread. I do not believe this is possible to do directly in Android - instead you must have the Activity start from the UI thread. Therefore, the way to do this in Android is to use a Handler. The Handler lives on the UI thread and receives messages from the background thread to do your UI actions such as starting another Activity.

Example:

//Inside your activity:
final Handler messageHandler = new Handler() {
    public void handleMessage(Message msg) 
    {
        //Start Activity
        Intent i = new Intent(Start.this, RadyoBabylonActivity.class);
        startActivity(i);
    }
}

//Inside onPrepared(MediaPlayer mp):
messageHandler.sendEmptyMessage(0);

See also: http://developer.android.com/reference/android/os/Handler.html

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