修复windows无法播放视频

This commit is contained in:
Hector 2025-12-04 15:27:01 +08:00
parent ee63a10c19
commit 2776c587c1
3 changed files with 42 additions and 18 deletions

View File

@ -33,8 +33,13 @@ class VideoManager:
# 创建视频纹理 # 创建视频纹理
if video_path and os.path.exists(video_path): if video_path and os.path.exists(video_path):
# Convert Windows path to Panda3D compatible path format
from panda3d.core import Filename
panda_path = Filename.fromOsSpecific(video_path)
converted_path = str(panda_path)
movie_texture = MovieTexture(name) movie_texture = MovieTexture(name)
success = movie_texture.read(video_path) success = movie_texture.read(converted_path)
if success: if success:
video_screen.setTexture(movie_texture) video_screen.setTexture(movie_texture)
@ -43,7 +48,7 @@ class VideoManager:
video_obj = { video_obj = {
'node': video_screen, 'node': video_screen,
'texture': movie_texture, 'texture': movie_texture,
'path': video_path, 'path': converted_path,
'name': name, 'name': name,
'type': 'movie', 'type': 'movie',
'is_playing': False 'is_playing': False
@ -62,7 +67,7 @@ class VideoManager:
print(f"✓ 视频屏幕创建成功: {name}") print(f"✓ 视频屏幕创建成功: {name}")
return video_screen return video_screen
else: else:
print(f"✗ 无法加载视频: {video_path}") print(f"✗ 无法加载视频: {converted_path}")
# 创建占位符纹理 # 创建占位符纹理
self._create_placeholder_texture(video_screen, name) self._create_placeholder_texture(video_screen, name)
else: else:
@ -103,8 +108,13 @@ class VideoManager:
# 创建视频纹理 # 创建视频纹理
if video_path and os.path.exists(video_path): if video_path and os.path.exists(video_path):
# Convert Windows path to Panda3D compatible path format
from panda3d.core import Filename
panda_path = Filename.fromOsSpecific(video_path)
converted_path = str(panda_path)
movie_texture = MovieTexture(name) movie_texture = MovieTexture(name)
success = movie_texture.read(video_path) success = movie_texture.read(converted_path)
if success: if success:
sphere.setTexture(movie_texture) sphere.setTexture(movie_texture)
@ -113,7 +123,7 @@ class VideoManager:
video_obj = { video_obj = {
'node': sphere, 'node': sphere,
'texture': movie_texture, 'texture': movie_texture,
'path': video_path, 'path': converted_path,
'name': name, 'name': name,
'type': 'spherical', 'type': 'spherical',
'is_playing': True 'is_playing': True

View File

@ -1375,8 +1375,13 @@ class GUIManager:
print(f"❌ 视频文件不存在: {video_path}") print(f"❌ 视频文件不存在: {video_path}")
return False return False
# Convert Windows path to Panda3D compatible path format
from panda3d.core import Filename
panda_path = Filename.fromOsSpecific(video_path)
converted_path = str(panda_path)
# 加载新的视频纹理 # 加载新的视频纹理
movie_texture = self._loadMovieTexture(video_path) movie_texture = self._loadMovieTexture(video_path) # Pass original path for existence check
if movie_texture: if movie_texture:
# 清除现有的纹理 # 清除现有的纹理
video_screen.clearTexture() video_screen.clearTexture()
@ -1405,15 +1410,15 @@ class GUIManager:
# 保存新的视频纹理引用到PythonTag # 保存新的视频纹理引用到PythonTag
video_screen.setPythonTag("movie_texture", movie_texture) video_screen.setPythonTag("movie_texture", movie_texture)
video_screen.setTag("video_path", video_path) video_screen.setTag("video_path", converted_path) # Store converted path
# 确保视频屏幕有正确的材质 # 确保视频屏幕有正确的材质
self._ensureVideoScreenMaterial(video_screen) self._ensureVideoScreenMaterial(video_screen)
print(f"✅ 成功加载新视频: {video_path}") print(f"✅ 成功加载新视频: {converted_path}")
return True return True
else: else:
print(f"❌ 无法加载视频文件: {video_path}") print(f"❌ 无法加载视频文件: {converted_path}")
return False return False
except Exception as e: except Exception as e:
@ -1425,7 +1430,7 @@ class GUIManager:
def _loadMovieTexture(self, video_path): def _loadMovieTexture(self, video_path):
"""加载视频纹理的兼容方法""" """加载视频纹理的兼容方法"""
try: try:
from panda3d.core import Texture, MovieTexture from panda3d.core import Texture, MovieTexture, Filename
import os import os
# 检查文件是否存在 # 检查文件是否存在
@ -1433,12 +1438,16 @@ class GUIManager:
print(f"❌ 视频文件不存在: {video_path}") print(f"❌ 视频文件不存在: {video_path}")
return None return None
print(f"🔍 尝试加载视频文件: {video_path}") # Convert Windows path to Panda3D compatible path format
panda_path = Filename.fromOsSpecific(video_path)
converted_path = str(panda_path)
print(f"🔍 尝试加载视频文件: {converted_path}")
# 方法1: 尝试使用 MovieTexture专门用于视频 # 方法1: 尝试使用 MovieTexture专门用于视频
try: try:
movie_texture = MovieTexture(video_path) movie_texture = MovieTexture(converted_path)
if movie_texture.read(video_path): if movie_texture.read(converted_path):
print("✅ 使用 MovieTexture 成功加载视频") print("✅ 使用 MovieTexture 成功加载视频")
self._configureVideoTexture(movie_texture) self._configureVideoTexture(movie_texture)
return movie_texture return movie_texture
@ -1449,7 +1458,7 @@ class GUIManager:
# 方法2: 尝试使用 loader.loadTexture # 方法2: 尝试使用 loader.loadTexture
try: try:
movie_texture = self.world.loader.loadTexture(video_path) movie_texture = self.world.loader.loadTexture(converted_path)
if movie_texture and hasattr(movie_texture, 'is_playable') and movie_texture.is_playable(): if movie_texture and hasattr(movie_texture, 'is_playable') and movie_texture.is_playable():
print("✅ 使用 loader.loadTexture 成功加载视频纹理") print("✅ 使用 loader.loadTexture 成功加载视频纹理")
self._configureVideoTexture(movie_texture) self._configureVideoTexture(movie_texture)
@ -1462,7 +1471,7 @@ class GUIManager:
# 方法3: 尝试使用 Texture.read作为最后备选 # 方法3: 尝试使用 Texture.read作为最后备选
try: try:
texture = Texture() texture = Texture()
if texture.read(video_path): if texture.read(converted_path):
print("✅ 使用 Texture.read 成功加载(可能作为静态纹理)") print("✅ 使用 Texture.read 成功加载(可能作为静态纹理)")
self._configureVideoTexture(texture) self._configureVideoTexture(texture)
return texture return texture

View File

@ -836,10 +836,15 @@ class MainApp(ShowBase):
return video_screen return video_screen
def _loadMovieTexture(self, video_path): def _loadMovieTexture(self, video_path):
from panda3d.core import Texture, MovieTexture from panda3d.core import Texture, MovieTexture, Filename
import os import os
movie_texture = MovieTexture(video_path)
if movie_texture.read(video_path): # Convert Windows path to Panda3D compatible path format
panda_path = Filename.fromOsSpecific(video_path)
converted_path = str(panda_path)
movie_texture = MovieTexture(converted_path)
if movie_texture.read(converted_path):
self._configureVideoTexture(movie_texture) self._configureVideoTexture(movie_texture)
return movie_texture return movie_texture