Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

On iPhone, ask to download a streaming book before exporting a clip

Barnabasc56c5b727b99
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index fdf23cec..4f5e063b 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -3685,6 +3685,7 @@
},
"clipExportClamped": "Clip saved, shortened to the end of this track",
"clipExportFailed": "Couldn't export the clip.",
+ "clipDownloadToExport": "Download this book first to export a clip on iPhone.",
"fsPickerTitle": "Select folder",
"fsServerRoot": "Server root",
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 3ab69e79..a30cba28 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -10770,6 +10770,12 @@ abstract class AppLocalizations {
/// **'Couldn\'t export the clip.'**
String get clipExportFailed;
+ /// No description provided for @clipDownloadToExport.
+ ///
+ /// In en, this message translates to:
+ /// **'Download this book first to export a clip on iPhone.'**
+ String get clipDownloadToExport;
+
/// No description provided for @fsPickerTitle.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index 8f3c778e..cf81320c 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -6234,6 +6234,10 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index 498b1c20..77d23a07 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -6167,6 +6167,10 @@ class AppLocalizationsEl extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index d08facb2..f54f77b1 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -6167,6 +6167,10 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 10e80b53..6ad4c110 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -6167,6 +6167,10 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index e0a5efd5..b1571dc6 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -6169,6 +6169,10 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index 51ac5ba8..c7647aa4 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -6167,6 +6167,10 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index 5503fb6c..a57d7020 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -6167,6 +6167,10 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index e413849d..cc5aafad 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -6167,6 +6167,10 @@ class AppLocalizationsNo extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index 778c0851..3c36d6ab 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -6167,6 +6167,10 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index c12e5734..0e421d85 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -6167,6 +6167,10 @@ class AppLocalizationsRo extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index e27c10a7..94b5af02 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -6170,6 +6170,10 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index 931e9168..265d04ab 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -6090,6 +6090,10 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get clipExportFailed => 'Couldn\'t export the clip.';
+ @override
+ String get clipDownloadToExport =>
+ 'Download this book first to export a clip on iPhone.';
+
@override
String get fsPickerTitle => 'Select folder';
diff --git a/lib/services/clip_export_service.dart b/lib/services/clip_export_service.dart
index 26bf4510..8a76d976 100644
--- a/lib/services/clip_export_service.dart
+++ b/lib/services/clip_export_service.dart
@@ -1,3 +1,5 @@
+import 'dart:io';
+
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
@@ -21,22 +23,27 @@ class ClipExportResult {
}
class ClipExportException implements Exception {
- ClipExportException(this.message);
+ ClipExportException(this.message, {this.code});
final String message;
+ /// Optional machine-readable reason so the UI can react specially (e.g.
+ /// 'needs_download' -> offer to download the book on iOS).
+ final String? code;
@override
String toString() => 'ClipExportException: $message';
}
/// Extracts a short audio clip starting at a bookmark and encodes it to .m4a
-/// (AAC) via native code - Android MediaCodec + MediaMuxer, iOS
-/// AVAssetExportSession. Works on downloaded files and streamed URLs (the
-/// native side range-requests just the needed window with the auth headers).
+/// (AAC) via native code (Android MediaCodec + MediaMuxer, iOS
+/// AVAssetExportSession). Downloaded books export from the local file on both
+/// platforms. Streamed books: Android's native side range-requests just the
+/// window from the URL; iOS can't export from a remote URL, so a streamed book
+/// must be downloaded first ([ClipExportException] code 'needs_download').
class ClipExportService {
static const _channel = MethodChannel('com.absorb.clip');
/// Build a clip for [itemId] starting at [startSeconds] (global book
/// position) running [requestedDuration] seconds. Throws
- /// [ClipExportException] on any failure so the UI can show one message.
+ /// [ClipExportException] on any failure so the UI can react.
Future<ClipExportResult> exportClip({
required String itemId,
required double startSeconds,
@@ -50,6 +57,13 @@ class ClipExportService {
final hit = BookTrackResolver.mapGlobal(tracks, startSeconds);
final track = hit.track;
+ // iOS can't extract a clip from a remote URL (neither AVAssetExportSession
+ // nor AVAssetReader handle it). Rather than silently download a whole file,
+ // ask the user to download the book first.
+ if (!track.local && Platform.isIOS) {
+ throw ClipExportException('book must be downloaded', code: 'needs_download');
+ }
+
// Don't run past the end of this track - cross-file stitching is a v1
// limitation, so shorten and tell the user.
var duration = requestedDuration;
@@ -67,7 +81,7 @@ class ClipExportService {
final outPath =
'${tmp.path}/absorb_clip_${DateTime.now().millisecondsSinceEpoch}.m4a';
- bool ok;
+ var ok = false;
try {
ok = await _channel.invokeMethod<bool>('exportClip', {
'source': track.source,
diff --git a/lib/widgets/clip_editor_sheet.dart b/lib/widgets/clip_editor_sheet.dart
index 121dce5e..ffc1c22d 100644
--- a/lib/widgets/clip_editor_sheet.dart
+++ b/lib/widgets/clip_editor_sheet.dart
@@ -3,11 +3,14 @@ import 'dart:typed_data';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
import '../l10n/app_localizations.dart';
+import '../providers/library_provider.dart';
import '../services/api_service.dart';
import '../services/clip_editor_controller.dart';
import '../services/clip_export_service.dart';
+import '../services/download_service.dart';
/// Play-and-mark clip editor. Opened from the bookmark sheet's "Export clip".
/// Plays a navigable window around the bookmark; the user marks in/out by ear
@@ -114,6 +117,11 @@ class _ClipEditorSheetState extends State<ClipEditorSheet> {
debugPrint('[ClipEditor] save failed: $e');
if (!mounted) return;
setState(() => _saving = false);
+ // iOS can't export from a streaming URL - offer to download the book.
+ if (e is ClipExportException && e.code == 'needs_download') {
+ await _promptDownload();
+ return;
+ }
// Show the reason in a dialog rather than a SnackBar: the editor is a
// bottom sheet, and on iPad a SnackBar shown from within it hides behind
// the sheet, so a failure looked like "nothing happened".
@@ -134,6 +142,56 @@ class _ClipEditorSheetState extends State<ClipEditorSheet> {
}
}
+ // iOS streamed export isn't possible, so offer to download the book; the user
+ // re-exports once it finishes. Cancel just closes the dialog.
+ Future<void> _promptDownload() async {
+ final l = AppLocalizations.of(context)!;
+ final go = await showDialog<bool>(
+ context: context,
+ builder: (ctx) => AlertDialog(
+ content: Text(l.clipDownloadToExport),
+ actions: [
+ TextButton(
+ onPressed: () => Navigator.pop(ctx, false),
+ child: Text(l.cancel),
+ ),
+ FilledButton(
+ onPressed: () => Navigator.pop(ctx, true),
+ child: Text(l.download),
+ ),
+ ],
+ ),
+ );
+ if (go == true) await _startDownload();
+ }
+
+ Future<void> _startDownload() async {
+ final api = widget.api;
+ if (api == null) return;
+ final navigator = Navigator.of(context);
+ final libraryId = context.read<LibraryProvider>().selectedLibraryId;
+ var title = widget.bookmarkTitle;
+ var author = '';
+ try {
+ final item = await api.getLibraryItem(widget.itemId);
+ final meta = (item?['media'] as Map<String, dynamic>?)?['metadata']
+ as Map<String, dynamic>?;
+ title = meta?['title'] as String? ?? title;
+ author = meta?['authorName'] as String? ?? '';
+ } catch (_) {}
+ await DownloadService().downloadItem(
+ api: api,
+ itemId: widget.itemId,
+ title: title,
+ author: author,
+ coverUrl: api.getCoverUrl(widget.itemId, width: 400),
+ libraryId: libraryId,
+ );
+ // Close the editor - the user re-exports once the download finishes (the
+ // download has its own progress notification).
+ if (mounted) navigator.pop();
+ }
+
@override
Widget build(BuildContext context) {
final l = AppLocalizations.of(context)!;