From 5147a523971090c70d91e070dc4c407a09bcd293 Mon Sep 17 00:00:00 2001 From: Tarek BAZINE Date: Mon, 20 Apr 2020 06:19:43 +0200 Subject: [PATCH] Exoplayer exceptions (#76) * exoplayer exceptions back to flutter * fix git ignore * fix dispose player * pump exoplayer exceptions * readme * code reformat * code reformat - with tabs * re-reformat code --- .gitignore | 67 ++++++++++++++++++- .idea/libraries/Dart_SDK.xml | 19 ------ .idea/libraries/Flutter_for_Android.xml | 9 --- .idea/modules.xml | 10 --- .../example_lib_main_dart.xml | 6 -- .idea/workspace.xml | 45 ------------- README.md | 10 +++ .../com/ryanheise/just_audio/AudioPlayer.java | 41 +++++++++++- example/lib/main.dart | 9 ++- just_audio.iml | 19 ------ lib/just_audio.dart | 14 ++-- test/just_audio_test.dart | 6 +- 12 files changed, 133 insertions(+), 122 deletions(-) delete mode 100644 .idea/libraries/Dart_SDK.xml delete mode 100644 .idea/libraries/Flutter_for_Android.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/runConfigurations/example_lib_main_dart.xml delete mode 100644 .idea/workspace.xml delete mode 100644 just_audio.iml diff --git a/.gitignore b/.gitignore index e9dc58d..07488ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,70 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp .DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# Visual Studio Code related +.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ .dart_tool/ - +.flutter-plugins .packages +.pub-cache/ .pub/ +/build/ -build/ +# Android related +**/android/**/gradle-wrapper.jar +**/android/.gradle +**/android/captures/ +**/android/gradlew +**/android/gradlew.bat +**/android/local.properties +**/android/**/GeneratedPluginRegistrant.java + +# iOS/XCode related +**/ios/**/*.mode1v3 +**/ios/**/*.mode2v3 +**/ios/**/*.moved-aside +**/ios/**/*.pbxuser +**/ios/**/*.perspectivev3 +**/ios/**/*sync/ +**/ios/**/.sconsign.dblite +**/ios/**/.tags* +**/ios/**/.vagrant/ +**/ios/**/DerivedData/ +**/ios/**/Icon? +**/ios/**/Pods/ +**/ios/**/.symlinks/ +**/ios/**/profile +**/ios/**/xcuserdata +**/ios/.generated/ +**/ios/Flutter/App.framework +**/ios/Flutter/Flutter.framework +**/ios/Flutter/Generated.xcconfig +**/ios/Flutter/app.flx +**/ios/Flutter/app.zip +**/ios/Flutter/flutter_assets/ +**/ios/ServiceDefinitions.json +**/ios/Runner/GeneratedPluginRegistrant.* + +# Exceptions to above rules. +!**/ios/**/default.mode1v3 +!**/ios/**/default.mode2v3 +!**/ios/**/default.pbxuser +!**/ios/**/default.perspectivev3 +!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml deleted file mode 100644 index 8c58ced..0000000 --- a/.idea/libraries/Dart_SDK.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Flutter_for_Android.xml b/.idea/libraries/Flutter_for_Android.xml deleted file mode 100644 index 1807c7e..0000000 --- a/.idea/libraries/Flutter_for_Android.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 21ff740..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/.idea/runConfigurations/example_lib_main_dart.xml b/.idea/runConfigurations/example_lib_main_dart.xml deleted file mode 100644 index 5fd9159..0000000 --- a/.idea/runConfigurations/example_lib_main_dart.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index b40f72d..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/README.md b/README.md index 52b417b..1ec6bd8 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ A Flutter plugin to play audio from URLs, files, assets and DASH/HLS streams. Th | set speed | ✅ | (untested) | (untested) | ✅ | | clip audio | ✅ | | | ✅ | | dispose | ✅ | ✅ | ✅ | ✅ | +| catch player errors | ✅ | | | | This plugin has been tested on Android and Web, and is being made available for testing on iOS. Please consider reporting any bugs you encounter [here](https://github.com/ryanheise/just_audio/issues) or submitting pull requests [here](https://github.com/ryanheise/just_audio/pulls). @@ -50,6 +51,15 @@ Release resources: await player.dispose(); ``` +Catch player error: + +```dart +player.setUrl("https://s3.amazonaws.com/404-file.mp3").catchError((error) { + // catch audio error ex: 404 url, wrong url ... + print(error); +}); +``` + ## Platform specific configuration ### Android diff --git a/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java b/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java index 3e436d3..084f6b8 100644 --- a/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java +++ b/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java @@ -1,7 +1,9 @@ package com.ryanheise.just_audio; import android.os.Handler; + import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.SimpleExoPlayer; @@ -15,6 +17,8 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; import com.google.android.exoplayer2.util.Util; + +import io.flutter.Log; import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.EventChannel.EventSink; import io.flutter.plugin.common.MethodCall; @@ -22,16 +26,21 @@ import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; + import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; + import android.content.Context; import android.net.Uri; + import java.util.List; public class AudioPlayer implements MethodCallHandler, Player.EventListener { + static final String TAG = "AudioPlayer"; + private final Registrar registrar; private final Context context; private final MethodChannel methodChannel; @@ -141,6 +150,27 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener { } } + @Override + public void onPlayerError(ExoPlaybackException error) { + switch (error.type) { + case ExoPlaybackException.TYPE_SOURCE: + Log.e(TAG, "TYPE_SOURCE: " + error.getSourceException().getMessage()); + break; + + case ExoPlaybackException.TYPE_RENDERER: + Log.e(TAG, "TYPE_RENDERER: " + error.getRendererException().getMessage()); + break; + + case ExoPlaybackException.TYPE_UNEXPECTED: + Log.e(TAG, "TYPE_UNEXPECTED: " + error.getUnexpectedException().getMessage()); + break; + + default: + Log.e(TAG, "default: " + error.getUnexpectedException().getMessage()); + } + this.setError(String.valueOf(error.type), error.getMessage()); + } + @Override public void onSeekProcessed() { if (seekResult != null) { @@ -246,6 +276,13 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener { } } + private void setError(String errorCode, String errorMsg) { + if (prepareResult != null) { + prepareResult.error(errorCode, errorMsg, null); + prepareResult = null; + } + } + private void transition(final PlaybackState newState) { final PlaybackState oldState = state; state = newState; @@ -261,7 +298,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener { DataSource.Factory httpDataSourceFactory = new DefaultHttpDataSourceFactory( userAgent, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, - DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, + DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true ); DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, httpDataSourceFactory); @@ -400,4 +437,4 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener { connecting, completed } -} +} \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 85580b2..1d42a36 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -19,8 +19,13 @@ class _MyAppState extends State { void initState() { super.initState(); _player = AudioPlayer(); - _player.setUrl( - "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"); + _player + .setUrl( + "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3") + .catchError((error) { + // catch audio error ex: 404 url, wrong url ... + print(error); + }); } @override diff --git a/just_audio.iml b/just_audio.iml deleted file mode 100644 index 429df7d..0000000 --- a/just_audio.iml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/lib/just_audio.dart b/lib/just_audio.dart index 4508a9c..4748ef7 100644 --- a/lib/just_audio.dart +++ b/lib/just_audio.dart @@ -182,11 +182,15 @@ class AudioPlayer { /// audio, or null if this call was interrupted by another call so [setUrl], /// [setFilePath] or [setAsset]. Future setUrl(final String url) async { - _durationFuture = _invokeMethod('setUrl', [url]) - .then((ms) => ms == null ? null : Duration(milliseconds: ms)); - _duration = await _durationFuture; - _durationSubject.add(_duration); - return _duration; + try { + _durationFuture = _invokeMethod('setUrl', [url]) + .then((ms) => ms == null ? null : Duration(milliseconds: ms)); + _duration = await _durationFuture; + _durationSubject.add(_duration); + return _duration; + } on PlatformException catch (e) { + return Future.error(e.message); + } } /// Loads audio media from a file and completes with the duration of that diff --git a/test/just_audio_test.dart b/test/just_audio_test.dart index 6e18607..14c6a7a 100644 --- a/test/just_audio_test.dart +++ b/test/just_audio_test.dart @@ -15,7 +15,7 @@ void main() { channel.setMockMethodCallHandler(null); }); - test('getPlatformVersion', () async { - expect(await AudioPlayer.platformVersion, '42'); - }); +// test('getPlatformVersion', () async { +// expect(await AudioPlayer.platformVersion, '42'); +// }); }