gpt4 book ai didi

android - 如何从线程开始在 Activity 中播放音乐?

转载 作者:行者123 更新时间:2023-11-29 01:48:03 26 4
gpt4 key购买 nike

我无法从线程启动 Activity 中的 MediaPlayer。我在我的 Android Activity 中加载了声音,称为 Snakes,并从自定义 View 类播放...

Activity Snakes()...
void playSound(int i){
if (sounds != null){
sounds.play(sound_questions[i], 1.0f, 1.0f, 0, 0, 1.0f);
}
}


CustomView()...
((Snakes)getContext()).playSound();

这有效。

我在 Activity 类中通过 MediaPlayer 播放音乐。为了延迟开始,我想使用从游戏控制线程调用的 mediaPlayer.Start()。我已经采用与声音播放相同的方法,并将其放入我的 Activity 类中的一个简单方法中

    void startMusic(){
mediaPlayer.start();
}

但是,我不能从线程中调用这个方法。

我应该如何从线程中启动 MediaPlayer?

最佳答案

在您的 Activity 中放置一个消息处理程序。

public class MyActivity extends Activity {

// Handler message constants
public static final int PLAY_SOUND = 0;
public static final int SOME_OTHER_MESSAGE = 1;
...

private final MainHandler mMainHandler = new MainHandler(this);

// Handler
private static class MainHandler extends Handler {

// Using a WeakReference is recommended to avoid memory leaks
private final WeakReference<MainActivity> mActivity;
public MainHandler(MainActivity activity) {
mActivity = new WeakReference<MainActivity>(activity);
}

// The message handler
@Override
public void handleMessage(Message msg) {
MainActivity activity = mActivity.get();
if (activity != null) {
final int index;
switch (msg.what) {
case PLAY_SOUND:
activity.playMySound();
break;
case SOME_OTHER_MESSAGE:
...
break;
...
}
}
}
};

private void playMySound() {
...
}
}

在您的线程中获取对处理程序的引用,并使用它向 Activity 发送消息。

public class MyThread extends Thread {

private Handler mParentHandler;

// Constructor
private MyThread(Handler parentHandler) {
mParentHandler = parentHandler;
}

...
// When you want to start playback
Message messageToMain = Message.obtain();
messageToMain.what = MainActivity.PLAY_SOUND;
mParentHandler.sendMessage(messageToMain);
...
}

或者,如果通过它的构造函数将主消息处理程序传递给线程不适合您的情况,您可以在线程中创建一个静态方法以在类级别将处理程序传递给它。

public class MyThread extends Thread {

// make mHandler static so it can be defined at a class level.
private static Handler mHandler;

// Constructor
private MyThread(...) {
// no need to pass Handler in constructor now
}

// static method for setting mHandler
public static void setHandler(Handler suppliedHandler) {
mHandler = suppliedHandler;
}

...
// When you want to start playback
Message messageToMain = Message.obtain();
messageToMain.what = MainActivity.PLAY_SOUND;
mHandler.sendMessage(messageToMain);
...
}

然后确保在创建线程的任何实例之前为线程设置要使用的处理程序(例如在 Activity 的 onCreate() 中执行)。

public class MyActivity extends Activity {

// Handler message constants
public static final int PLAY_SOUND = 0;
public static final int SOME_OTHER_MESSAGE = 1;
...

private final MainHandler mMainHandler = new MainHandler(this);

// Handler
private static class MainHandler extends Handler {

// Using a WeakReference is recommended to avoid memory leaks
private final WeakReference<MainActivity> mActivity;
public MainHandler(MainActivity activity) {
mActivity = new WeakReference<MainActivity>(activity);
}

// The message handler
@Override
public void handleMessage(Message msg) {
MainActivity activity = mActivity.get();
if (activity != null) {
final int index;
switch (msg.what) {
case PLAY_SOUND:
activity.playMySound();
break;
case SOME_OTHER_MESSAGE:
...
break;
...
}
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
MyThread.setHandler(mMainHandler);
// setting this at a class level means all future instances of MyThread
// will have their mHandler set to mMainHandler
...
}

private void playMySound() {
...
}
}

关于android - 如何从线程开始在 Activity 中播放音乐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20181297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com