Flutter : 分享一条记录
Flutter : share a record
我正在使用 record_mp3 package 录制音频文件,我可以通过 whatsapp 和其他社交媒体分享这段录音吗?
提前致谢
您可以复制粘贴运行下面的完整代码
您可以使用包 https://pub.dev/packages/esys_flutter_share
代码片段
onPressed: () async {
print('recordFilePath ${recordFilePath}');
File file = File(recordFilePath);
Uint8List bytes = await file.readAsBytes() as Uint8List;
await Share.file(
'yourmusic', 'yourmusic.mp3', bytes, 'audio/mpeg');
}
工作演示
完整代码
import 'dart:io';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:record_mp3/record_mp3.dart';
import 'dart:io';
import 'dart:typed_data';
import 'package:esys_flutter_share/esys_flutter_share.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String statusText = "";
bool isComplete = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: GestureDetector(
child: Container(
height: 48.0,
decoration: BoxDecoration(color: Colors.red.shade300),
child: Center(
child: Text(
'start',
style: TextStyle(color: Colors.white),
),
),
),
onTap: () async {
startRecord();
},
),
),
Expanded(
child: GestureDetector(
child: Container(
height: 48.0,
decoration: BoxDecoration(color: Colors.blue.shade300),
child: Center(
child: Text(
RecordMp3.instance.status == RecordStatus.PAUSE
? 'resume'
: 'pause',
style: TextStyle(color: Colors.white),
),
),
),
onTap: () {
pauseRecord();
},
),
),
Expanded(
child: GestureDetector(
child: Container(
height: 48.0,
decoration: BoxDecoration(color: Colors.green.shade300),
child: Center(
child: Text(
'stop',
style: TextStyle(color: Colors.white),
),
),
),
onTap: () {
stopRecord();
},
),
),
],
),
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text(
statusText,
style: TextStyle(color: Colors.red, fontSize: 20),
),
),
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
play();
},
child: Container(
margin: EdgeInsets.only(top: 30),
alignment: AlignmentDirectional.center,
width: 100,
height: 50,
child: isComplete && recordFilePath != null
? Text(
"播放",
style: TextStyle(color: Colors.red, fontSize: 20),
)
: Container(),
),
),
]),
floatingActionButton: FloatingActionButton(
onPressed: () async {
print('recordFilePath ${recordFilePath}');
File file = File(recordFilePath);
Uint8List bytes = await file.readAsBytes() as Uint8List;
await Share.file(
'yourmusic', 'yourmusic.mp3', bytes, 'audio/mpeg');
},
child: Icon(Icons.share),
backgroundColor: Colors.green,
)),
);
}
Future<bool> checkPermission() async {
Map<PermissionGroup, PermissionStatus> map = await new PermissionHandler()
.requestPermissions(
[PermissionGroup.storage, PermissionGroup.microphone]);
print(map[PermissionGroup.microphone]);
return map[PermissionGroup.microphone] == PermissionStatus.granted;
}
void startRecord() async {
bool hasPermission = await checkPermission();
if (hasPermission) {
statusText = "正在录音中...";
recordFilePath = await getFilePath();
isComplete = false;
RecordMp3.instance.start(recordFilePath, (type) {
statusText = "录音失败--->$type";
setState(() {});
});
} else {
statusText = "没有录音权限";
}
setState(() {});
}
void pauseRecord() {
if (RecordMp3.instance.status == RecordStatus.PAUSE) {
bool s = RecordMp3.instance.resume();
if (s) {
statusText = "正在录音中...";
setState(() {});
}
} else {
bool s = RecordMp3.instance.pause();
if (s) {
statusText = "录音暂停中...";
setState(() {});
}
}
}
void stopRecord() {
bool s = RecordMp3.instance.stop();
if (s) {
statusText = "录音已完成";
isComplete = true;
setState(() {});
}
}
void resumeRecord() {
bool s = RecordMp3.instance.resume();
if (s) {
statusText = "正在录音中...";
setState(() {});
}
}
String recordFilePath;
void play() {
if (recordFilePath != null && File(recordFilePath).existsSync()) {
AudioPlayer audioPlayer = AudioPlayer();
audioPlayer.play(recordFilePath, isLocal: true);
}
}
int i = 0;
Future<String> getFilePath() async {
Directory storageDirectory = await getApplicationDocumentsDirectory();
String sdPath = storageDirectory.path + "/record";
var d = Directory(sdPath);
if (!d.existsSync()) {
d.createSync(recursive: true);
}
return sdPath + "/test_${i++}.mp3";
}
}
我正在使用 record_mp3 package 录制音频文件,我可以通过 whatsapp 和其他社交媒体分享这段录音吗?
提前致谢
您可以复制粘贴运行下面的完整代码
您可以使用包 https://pub.dev/packages/esys_flutter_share
代码片段
onPressed: () async {
print('recordFilePath ${recordFilePath}');
File file = File(recordFilePath);
Uint8List bytes = await file.readAsBytes() as Uint8List;
await Share.file(
'yourmusic', 'yourmusic.mp3', bytes, 'audio/mpeg');
}
工作演示
完整代码
import 'dart:io';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:record_mp3/record_mp3.dart';
import 'dart:io';
import 'dart:typed_data';
import 'package:esys_flutter_share/esys_flutter_share.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String statusText = "";
bool isComplete = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: GestureDetector(
child: Container(
height: 48.0,
decoration: BoxDecoration(color: Colors.red.shade300),
child: Center(
child: Text(
'start',
style: TextStyle(color: Colors.white),
),
),
),
onTap: () async {
startRecord();
},
),
),
Expanded(
child: GestureDetector(
child: Container(
height: 48.0,
decoration: BoxDecoration(color: Colors.blue.shade300),
child: Center(
child: Text(
RecordMp3.instance.status == RecordStatus.PAUSE
? 'resume'
: 'pause',
style: TextStyle(color: Colors.white),
),
),
),
onTap: () {
pauseRecord();
},
),
),
Expanded(
child: GestureDetector(
child: Container(
height: 48.0,
decoration: BoxDecoration(color: Colors.green.shade300),
child: Center(
child: Text(
'stop',
style: TextStyle(color: Colors.white),
),
),
),
onTap: () {
stopRecord();
},
),
),
],
),
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text(
statusText,
style: TextStyle(color: Colors.red, fontSize: 20),
),
),
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
play();
},
child: Container(
margin: EdgeInsets.only(top: 30),
alignment: AlignmentDirectional.center,
width: 100,
height: 50,
child: isComplete && recordFilePath != null
? Text(
"播放",
style: TextStyle(color: Colors.red, fontSize: 20),
)
: Container(),
),
),
]),
floatingActionButton: FloatingActionButton(
onPressed: () async {
print('recordFilePath ${recordFilePath}');
File file = File(recordFilePath);
Uint8List bytes = await file.readAsBytes() as Uint8List;
await Share.file(
'yourmusic', 'yourmusic.mp3', bytes, 'audio/mpeg');
},
child: Icon(Icons.share),
backgroundColor: Colors.green,
)),
);
}
Future<bool> checkPermission() async {
Map<PermissionGroup, PermissionStatus> map = await new PermissionHandler()
.requestPermissions(
[PermissionGroup.storage, PermissionGroup.microphone]);
print(map[PermissionGroup.microphone]);
return map[PermissionGroup.microphone] == PermissionStatus.granted;
}
void startRecord() async {
bool hasPermission = await checkPermission();
if (hasPermission) {
statusText = "正在录音中...";
recordFilePath = await getFilePath();
isComplete = false;
RecordMp3.instance.start(recordFilePath, (type) {
statusText = "录音失败--->$type";
setState(() {});
});
} else {
statusText = "没有录音权限";
}
setState(() {});
}
void pauseRecord() {
if (RecordMp3.instance.status == RecordStatus.PAUSE) {
bool s = RecordMp3.instance.resume();
if (s) {
statusText = "正在录音中...";
setState(() {});
}
} else {
bool s = RecordMp3.instance.pause();
if (s) {
statusText = "录音暂停中...";
setState(() {});
}
}
}
void stopRecord() {
bool s = RecordMp3.instance.stop();
if (s) {
statusText = "录音已完成";
isComplete = true;
setState(() {});
}
}
void resumeRecord() {
bool s = RecordMp3.instance.resume();
if (s) {
statusText = "正在录音中...";
setState(() {});
}
}
String recordFilePath;
void play() {
if (recordFilePath != null && File(recordFilePath).existsSync()) {
AudioPlayer audioPlayer = AudioPlayer();
audioPlayer.play(recordFilePath, isLocal: true);
}
}
int i = 0;
Future<String> getFilePath() async {
Directory storageDirectory = await getApplicationDocumentsDirectory();
String sdPath = storageDirectory.path + "/record";
var d = Directory(sdPath);
if (!d.existsSync()) {
d.createSync(recursive: true);
}
return sdPath + "/test_${i++}.mp3";
}
}