Skip to content

Commit

Permalink
实现播放指定路径文件
Browse files Browse the repository at this point in the history
  • Loading branch information
yxwandroid committed Dec 19, 2019
1 parent 169430f commit 8c0cc47
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.1.2

* 实现播放指定路径录音文件
## 0.1.1

* 格式代码
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ stopRecord 停止录制的回调 返回的path是录制成功之后文件的保
## 更新内容

- [x] 实现录制时长的监听

- [x] 实现播放指定路径音频文件

## TODO

* [ ] 实现发送语音时间按下抬起时间很短提示
* [ ] 支持mp3录制




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class FlutterPluginRecordPlugin: MethodCallHandler ,PluginRegistry.RequestPermis
"start" -> start()
"stop" -> stop()
"play" -> play()
"playByPath" -> playByPath()
else -> result.notImplemented()
}
}
Expand Down Expand Up @@ -83,6 +84,18 @@ class FlutterPluginRecordPlugin: MethodCallHandler ,PluginRegistry.RequestPermis
channel.invokeMethod("onPlay", m1)
}

private fun playByPath() {
val path = _call.argument<String>("path")
val recorderUtil = RecorderUtil(path)
recorderUtil.playVoice()

Log.d("android voice ", "play")
val _id = _call.argument<String>("id")
val m1 = HashMap<String, String>()
m1["id"] = _id!!
channel.invokeMethod("onPlay", m1)
}

private fun stop() {
if(audioHandler!=null){
if(audioHandler?.isRecording ==true){
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: b6a0a141693093b304368d08511b46cf3d1d0ac5

COCOAPODS: 1.8.0
COCOAPODS: 1.8.4
15 changes: 14 additions & 1 deletion example/lib/second_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class SecondScreen extends StatefulWidget {
class _SecondScreenState extends State<SecondScreen> {
FlutterPluginRecord recordPlugin = new FlutterPluginRecord();

String filePath ="";

@override
void initState() {
Expand All @@ -28,7 +29,8 @@ class _SecondScreenState extends State<SecondScreen> {
recordPlugin.response.listen((data) {
if (data.msg == "onStop") {
///结束录制时会返回录制文件的地址方便上传服务器
print("onStop 时长" + data.path);
print("onStop 文件路径" + data.path);
filePath = data.path;
print("onStop 时长 " + data.audioTimeLength.toString());
} else if (data.msg == "onStart") {
print("onStart --");
Expand Down Expand Up @@ -79,6 +81,12 @@ class _SecondScreenState extends State<SecondScreen> {
play();
},
),
FlatButton(
child: Text("播放本地指定路径录音文件"),
onPressed: () {
playByPath(filePath);
},
),
],
),
),
Expand All @@ -104,6 +112,11 @@ class _SecondScreenState extends State<SecondScreen> {
void play() {
recordPlugin.play();
}
///播放指定路径录音文件
void playByPath(String path) {
recordPlugin.playByPath(path);
}


@override
void dispose() {
Expand Down
22 changes: 21 additions & 1 deletion ios/Classes/FlutterPluginRecordPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
[self stop ];
}else if([@"play" isEqualToString:method]){
[self play ];
}else if([@"playByPath" isEqualToString:method]){
[self playByPath];
}else{
result(FlutterMethodNotImplemented);
}

}

- (void) initRecord{
Expand Down Expand Up @@ -127,5 +129,23 @@ - (void) play{
[_channel invokeMethod:@"onPlay" arguments:dict3];
}

- (void) playByPath{

NSLog(@"ios voice play");
NSDictionary *args = [_call arguments];
NSString *filePath = [args valueForKey:@"path"];
NSData* data= [NSData dataWithContentsOfFile:filePath];

[DPAudioPlayer.sharedInstance startPlayWithData:data];
DPAudioPlayer.sharedInstance.playComplete = ^void(){
NSLog(@"播放完成");
};

NSString *mId = [args valueForKey:@"id"];
NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"success",@"result",mId,@"id", nil];
[_channel invokeMethod:@"onPlay" arguments:dict3];
}


@end

7 changes: 7 additions & 0 deletions lib/flutter_plugin_record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ class FlutterPluginRecord {
});
}

Future playByPath(String path) async {
return await _invokeMethod('playByPath', <String, String>{
"play": "play",
"path": path,
});
}

dispose() {
_responseInitController.close();
_responseController.close();
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: flutter_plugin_record
description: The flutter voice recording plug-in,provides the recording animation and the recording successfully returns to the recording file path
version: 0.1.1
version: 0.1.2
#author: wilson <[email protected]>
homepage: https://github.com/yxwandroid/flutter_plugin_record

Expand Down

0 comments on commit 8c0cc47

Please sign in to comment.