Track down and hopefully fix some problems with memory management.
This commit is contained in:
@@ -32,6 +32,8 @@ class MpvPlayer:
|
||||
self.endFileCallback = None # Callback for when file finishes
|
||||
self.playlist = [] # Current playlist (for multi-file audiobooks)
|
||||
self.currentPlaylistIndex = 0 # Current file index in playlist
|
||||
self.activeTempFiles = [] # Track temp files for cleanup
|
||||
self.tempFileLock = threading.Lock() # Protect temp file list
|
||||
|
||||
if not HAS_MPV:
|
||||
print("Warning: python-mpv not installed. Audio playback will not work.")
|
||||
@@ -90,21 +92,31 @@ class MpvPlayer:
|
||||
if playbackSpeed is None:
|
||||
playbackSpeed = self.playbackSpeed
|
||||
|
||||
# Track this temp file for cleanup
|
||||
with self.tempFileLock:
|
||||
self.activeTempFiles.append(tempFile.name)
|
||||
|
||||
# Load and play the temp file
|
||||
success = self.load_audio_file(tempFile.name, playbackSpeed=playbackSpeed)
|
||||
if success:
|
||||
success = self.play_audio_file()
|
||||
|
||||
# Clean up temp file after a delay (mpv needs time to load it)
|
||||
# Schedule cleanup after mpv loads the file
|
||||
# Use shorter delay and clean up old files too
|
||||
if tempFile:
|
||||
import threading
|
||||
import time
|
||||
def cleanup_temp_file(filepath):
|
||||
time.sleep(5) # Wait for mpv to fully load the file
|
||||
time.sleep(2) # Reduced from 5s - mpv loads files quickly
|
||||
try:
|
||||
os.unlink(filepath)
|
||||
except:
|
||||
pass
|
||||
# Remove from active list
|
||||
with self.tempFileLock:
|
||||
try:
|
||||
self.activeTempFiles.remove(filepath)
|
||||
except ValueError:
|
||||
pass # Already removed
|
||||
threading.Thread(target=cleanup_temp_file, args=(tempFile.name,), daemon=True).start()
|
||||
|
||||
return success
|
||||
@@ -131,6 +143,8 @@ class MpvPlayer:
|
||||
if self.isInitialized and self.player:
|
||||
self.player.stop()
|
||||
self.isPaused = False
|
||||
# Immediately clean up any temp files
|
||||
self._cleanup_temp_files()
|
||||
|
||||
def is_playing(self):
|
||||
"""Check if audio is currently playing"""
|
||||
@@ -247,6 +261,19 @@ class MpvPlayer:
|
||||
except Exception as e:
|
||||
print(f"Error setting volume: {e}")
|
||||
|
||||
def _cleanup_temp_files(self):
|
||||
"""Immediately clean up all active temp files"""
|
||||
with self.tempFileLock:
|
||||
for tempPath in self.activeTempFiles:
|
||||
try:
|
||||
if os.path.exists(tempPath):
|
||||
os.unlink(tempPath)
|
||||
except Exception as e:
|
||||
# Ignore errors - file might already be deleted
|
||||
pass
|
||||
# Clear the list
|
||||
self.activeTempFiles = []
|
||||
|
||||
def cleanup(self):
|
||||
"""Cleanup resources"""
|
||||
if self.isInitialized and self.player:
|
||||
@@ -259,6 +286,8 @@ class MpvPlayer:
|
||||
except:
|
||||
pass
|
||||
self.isInitialized = False
|
||||
# Clean up any remaining temp files
|
||||
self._cleanup_temp_files()
|
||||
|
||||
def is_available(self):
|
||||
"""Check if mpv is available"""
|
||||
@@ -420,6 +449,8 @@ class MpvPlayer:
|
||||
except:
|
||||
pass
|
||||
self.isPaused = False
|
||||
# Clean up temp files when stopping
|
||||
self._cleanup_temp_files()
|
||||
|
||||
def is_audio_file_playing(self):
|
||||
"""Check if audio file is currently playing"""
|
||||
|
||||
Reference in New Issue
Block a user