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
This commit is contained in:
Tarek BAZINE 2020-04-20 06:19:43 +02:00 committed by GitHub
parent e02f59cd31
commit 5147a52397
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 133 additions and 122 deletions

67
.gitignore vendored
View File

@ -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

View File

@ -1,19 +0,0 @@
<component name="libraryTable">
<library name="Dart SDK">
<CLASSES>
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/async" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/collection" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/convert" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/core" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/developer" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/html" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/io" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/isolate" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/math" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/mirrors" />
<root url="file:///home/ryan/opt/flutter/bin/cache/dart-sdk/lib/typed_data" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -1,9 +0,0 @@
<component name="libraryTable">
<library name="Flutter for Android">
<CLASSES>
<root url="jar:///home/ryan/opt/flutter/bin/cache/artifacts/engine/android-arm/flutter.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/just_audio.iml" filepath="$PROJECT_DIR$/just_audio.iml" />
<module fileurl="file://$PROJECT_DIR$/android/just_audio_android.iml" filepath="$PROJECT_DIR$/android/just_audio_android.iml" />
<module fileurl="file://$PROJECT_DIR$/example/android/just_audio_example_android.iml" filepath="$PROJECT_DIR$/example/android/just_audio_example_android.iml" />
</modules>
</component>
</project>

View File

@ -1,6 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="example/lib/main.dart" type="FlutterRunConfigurationType" factoryName="Flutter">
<option name="filePath" value="$PROJECT_DIR$/example/lib/main.dart" />
<method />
</configuration>
</component>

View File

@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="FileEditorManager">
<leaf>
<file leaf-file-name="just_audio.dart" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/lib/just_audio.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0">
<caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="main.dart" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/example/lib/main.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0">
<caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="ToolWindowManager">
<editor active="true" />
<layout>
<window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
</layout>
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1">
</navigator>
<panes>
<pane id="ProjectPane">
<option name="show-excluded-files" value="false" />
</pane>
</panes>
</component>
<component name="PropertiesComponent">
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="dart.analysis.tool.window.force.activate" value="true" />
<property name="show.migrate.to.gradle.popup" value="false" />
</component>
</project>

View File

@ -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

View File

@ -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
}
}
}

View File

@ -19,8 +19,13 @@ class _MyAppState extends State<MyApp> {
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

View File

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/example/.pub" />
<excludeFolder url="file://$MODULE_DIR$/example/build" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart Packages" level="project" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Flutter Plugins" level="project" />
</component>
</module>

View File

@ -182,11 +182,15 @@ class AudioPlayer {
/// audio, or null if this call was interrupted by another call so [setUrl],
/// [setFilePath] or [setAsset].
Future<Duration> 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

View File

@ -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');
// });
}