gpt4 book ai didi

java - 当线程在后台运行时,MediaPlayer 不显示视频

转载 作者:行者123 更新时间:2023-12-01 15:33:16 25 4
gpt4 key购买 nike

我有一个 MediaPlayer 正在播放文件,并且我想在后台连续写入日志数据。这应该由 Thread (或类似的概念,但我不知道 AsyncTask 是否是这里的方法)来完成。线程不应该总是写入,而应该在中间暂停半秒。

我的 MediaPlayer 工作正常,但是一旦我尝试运行我的线程,就不会显示任何视频。不过该线程似乎正在运行。

相关代码如下:

主题:

public class LoggingThread implements Runnable {
String videoName;

public LoggingThread(String videoName) {
Logger.startContinuousLogCSV(videoName);
Log.d(TAG, "Created logging thread");
}

public void run() {
Log.d(TAG, "Trying to run the thread");
try {
while (mIsVideoPlaying) {
Log.d(TAG, "Video is playing. Writing rating to file.");
// write to file and Thread.sleep(500)
// problem occurs regardless of this
}
Log.d(TAG, "Video not playing anymore, stopping thread.");
} catch (Exception e) {
Logger.closeContinuousLogCSV();
}
}
}

媒体播放器启动:

这里是我为视频准备播放器并创建 Thread 实例的地方。

public void preparePlayerForVideo(int videoIndex) {
Log.d(TAG, "Creating new logging thread");
mLoggingThread = new LoggingThread(Session.sTracks.get(videoIndex));

mPlayer = new MediaPlayer();
// various calls to set up player, this works fine
mPlayer.setOnPreparedListener(this);
mPlayer.prepare();
}

这是从onPreparedListener调用的。

public void startVideo() {
mIsVideoPlaying = true;
mLoggingThread.run();
mPlayer.start();
}

LogCat 输出:

从输出来看,线程运行良好,但为什么播放器不显示任何视频?

02-15 12:00:18.904: D/SubjectivePlayerSession(17918): Creating new logging thread
02-15 12:00:18.944: D/SubjectivePlayerSession(17918): Created logging thread
02-15 12:00:18.944: D/SubjectivePlayerSession(17918): Set data source to: /mnt/sdcard/SubjectiveMovies/pair_01-video_a-GUT_DEUTSCH.mp4
02-15 12:00:19.095: V/SubjectivePlayerSession(17918): onVideoSizeChanged called
02-15 12:00:19.095: D/SubjectivePlayerSession(17918): Running thread
02-15 12:00:19.095: D/SubjectivePlayerSession(17918): Trying to run the thread
02-15 12:00:19.095: D/SubjectivePlayerSession(17918): Video is playing. Writing rating to file.
02-15 12:00:19.605: D/SubjectivePlayerSession(17918): Video is playing. Writing rating to file.
...

最佳答案

您应该启动一个Thread对象并向其中添加您的Runnable:

public void startVideo() {
mIsVideoPlaying = true;
new Thread(mLoggingThread).start();
mPlayer.start();
}

调用 Runnable#run() 将在当前线程上运行代码,而不是创建新线程。

关于java - 当线程在后台运行时,MediaPlayer 不显示视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9292401/

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