Skip to content

Commit

Permalink
Supports playing local directory media files (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromexiong committed Mar 10, 2020
1 parent a44c639 commit 286453e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.1+1

- Supports playing local directory media files

## 0.5.1

- Add internal playlist management
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ Since `Android9.0 (API 28)`, the application disables HTTP plaintext requests by
The `audio_manager` plugin is developed in singleton mode. You only need to get`AudioManager.instance` in the method to quickly start using it.

## Quick start
⚠️ you can use local `assets` resources or `network` resources
⚠️ you can use local `assets`, `network` or `directory file` resources

```dart
// Initial playback. Preloaded playback information
AudioManager.instance
.start(
"assets/audio.mp3",
// "network format resource"
// "local resource (file://${file.path})"
"title",
desc: "desc",
cover: "assets/ic_launcher.png",
Expand Down
18 changes: 16 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:audio_manager/audio_manager.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(MyApp());

Expand Down Expand Up @@ -31,7 +33,14 @@ class _MyAppState extends State<MyApp> {
{
"title": "network",
"desc": "network resouce playback",
"url": "https://www.kozco.com/tech/piano2-CoolEdit.mp3",
"url": "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.m4a",
"cover":
"/static/img/pub-dev-logo.svg?hash=40fqenbgtbjcekk60vd5dg5mr22bv99t"
},
{
"title": "file",
"desc": "local file",
"url": "test.mp3",
"cover":
"https://cdn.jsdelivr.net/gh/flutterchina/website@1.0/images/flutter-mark-square-100.png"
},
Expand All @@ -52,10 +61,15 @@ class _MyAppState extends State<MyApp> {
super.dispose();
}

void setupAudio() {
void setupAudio() async {
List<AudioInfo> list = [];
this.list.forEach((item) => list.add(AudioInfo(item["url"],
title: item["title"], desc: item["desc"], coverUrl: item["cover"])));
final appDocDir = await getApplicationDocumentsDirectory();
// Please make sure the `test.mp3` exists in the document directory
final file = File("${appDocDir.path}/test.mp3");
list.last.url = "file://${file.path}";

AudioManager.instance.audioList = list;
AudioManager.instance.intercepter = true;
AudioManager.instance.play(auto: false);
Expand Down
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dev_dependencies:
flutter_test:
sdk: flutter

path_provider: ^1.6.5
audio_manager:
path: ../

Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/AudioManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ open class AudioManager: NSObject {
}
playerItem = AVPlayerItem(url: URL(fileURLWithPath: path))
}else {
guard let path = transformURLString(link)?.url, let _ = path.host else {
guard let path = transformURLString(link)?.url else {
onEvents?(.error(NSError(domain: domain, code: -1, userInfo: ["msg": "link [\(link)] is invalid"])))
return
}
Expand Down
11 changes: 10 additions & 1 deletion lib/src/index.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:audio_manager/src/PlayMode.dart';
import 'package:audio_manager/src/AudioInfo.dart';
Expand Down Expand Up @@ -184,6 +185,14 @@ class AudioManager {
return await play(index: 0, auto: auto);
}

/// This will load the file from the file-URI given by:
/// `'file://${file.path}'`.
Future<String> file(File file, String title,
{String desc, String cover, bool auto}) async {
return start("file://${file.path}", title,
desc: desc, cover: cover, auto: auto);
}

/// Play specified subscript audio if you want
Future<String> play({int index, bool auto}) async {
if (index != null && (index < 0 || index >= _audioList.length))
Expand All @@ -192,7 +201,7 @@ class AudioManager {
_curIndex = index ?? _curIndex;
_info = _initRandom();

final regx = new RegExp(r'^(http|https):\/\/([\w.]+\/?)\S*');
final regx = new RegExp(r'^(http|https|file):\/\/\/?([\w.]+\/?)\S*');
final result = await _channel.invokeMethod('start', {
"url": _info.url,
"title": _info.title,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: audio_manager
description: A flutter plugin for music playback, including notification handling.
version: 0.5.1
version: 0.5.1+1
homepage: https://github.com/jeromexiong/audio_manager

environment:
Expand Down

0 comments on commit 286453e

Please sign in to comment.