Skip to content

Commit

Permalink
committed files I changed. I messed up my directory. I have to rebuil…
Browse files Browse the repository at this point in the history
…d it
  • Loading branch information
hungrymonkey committed Aug 6, 2017
1 parent e5e5683 commit 5132420
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 7 deletions.
1 change: 1 addition & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ android {

// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.yourcompany.micstreamexample"
minSdkVersion 21
}

buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
android:versionCode="1"
android:versionName="0.0.1">

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" />

<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
package com.yourcompany.micstreamexample;

import android.media.AudioAttributes;
import android.media.AudioTrack;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.os.Bundle;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugins.GeneratedPluginRegistrant;

import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import java.util.HashMap;
public class MainActivity extends FlutterActivity {
private static final String AUDIO_CHANNEL = "com.yourcompany.flutter/audioFragmentPlayer";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
final FragmentPlayer fPlayer = new FragmentPlayer();
new MethodChannel(getFlutterView(), AUDIO_CHANNEL).setMethodCallHandler(
new MethodCallHandler(){
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("play")) {
HashMap<String, byte[]> args = (HashMap) call.arguments();
fPlayer.play(args.get("audioFragment"));
result.success(1);
} if(call.method.equals("stop")) {
fPlayer.stop();
result.success(1);
} else {
result.notImplemented();
}
}
}
);
}
}

class FragmentPlayer{
private int sampleRate;
private AudioTrack audioTrack;
private AudioFormat audioFormat;
FragmentPlayer(){
sampleRate = 48000;
audioFormat = new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT).setSampleRate(sampleRate)
.setSampleRate(AudioFormat.CHANNEL_OUT_MONO).build();
}
public void play(byte [] audioFrag){
if(audioTrack == null){
AudioAttributes aa = new AudioAttributes.Builder().build();
audioTrack = new AudioTrack( aa, new AudioFormat.Builder(audioFormat).build(), audioFrag.length,
AudioTrack.MODE_STREAM, AudioManager.AUDIO_SESSION_ID_GENERATE);
}
audioTrack.write(audioFrag,0,audioFrag.length);
audioTrack.play();
}
public void stop(){
audioTrack.stop();
audioTrack.release();
audioTrack = null;
}
}

45 changes: 45 additions & 0 deletions example/lib/fragment_player.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:flutter/foundation.dart';

import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/services.dart';

enum PlayerState { stopped, playing, paused }

class FragmentPlayer{
static const String FROG_CHANNEL = "com.yourcompany.flutter/audioFragmentPlayer";
static const platform = const MethodChannel(FROG_CHANNEL);

PlayerState _pState;

FragmentPlayer(): _pState = PlayerState.stopped{
}

Future<Null> start() async {
if(_pState != PlayerState.playing){
_pState = PlayerState.playing;
}
}
Future<Null> stream(Uint16List fragments) async{
if(_pState == PlayerState.playing) {
//covert to byte list as limited by platform channels supported type
// https://flutter.io/platform-channels/#codec
Uint8List byteFrags = new Uint8List(fragments.length * 2);
for (var i = 0; i < fragments.length; i++) {
byteFrags[i] = (fragments[i] & 0xff);
byteFrags[i + 1] = ((fragments[i] >> 8) & 0xff);
}

final int result = await platform.invokeMethod('play', byteFrags);
}
}
Future<Null> stop() async{
if(_pState != PlayerState.stopped ) {
final int result = await platform.invokeMethod('stop');
_pState = result == 1 ? PlayerState.stopped : _pState;
}
}



}
24 changes: 21 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'dart:async';
import 'dart:typed_data';

import 'package:mic_stream/mic_stream.dart';

import './fragment_player.dart';
void main() {
runApp(new MyApp());
}
Expand All @@ -15,13 +17,19 @@ class MyApp extends StatefulWidget {

class _MyAppState extends State<MyApp> {
List<StreamSubscription<dynamic>> _micStreamSubscription = <StreamSubscription<dynamic>>[];
Uint16List _micChunk= null;
Uint16List _micAudioFragment= null;
FragmentPlayer _fPlayer;
int _counter;
@override
initState() {
super.initState();
_fPlayer = new FragmentPlayer();
_counter = 0;
_micStreamSubscription.add(micEvents.listen((MicEvent e){
setState((){
_micChunk = e.audioData;
_micAudioFragment = e.audioData;
//_fPlayer.stream(_micAudioFragment);
_counter++;
});
}));
}
Expand All @@ -45,7 +53,17 @@ class _MyAppState extends State<MyApp> {
title: new Text('Plugin example app'),
),
body: new Center(
child: new Text('Running on: ${_micChunk.toString()}\n'),
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
new IconButton(icon: new Icon(Icons.play_arrow), onPressed: (){_fPlayer.start();}),
new IconButton(icon: new Icon(Icons.stop), onPressed: (){_fPlayer.stop();}),
],
),
new Text('Running on: ${_micAudioFragment.toString()}\n'),
],
),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion example/mic_stream_example.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="FLUTTER_MODULE_TYPE" version="4">
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
Expand Down
2 changes: 1 addition & 1 deletion lib/mic_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/services.dart';


const EventChannel _recordEventChannel =
const EventChannel('com.yourcompany.micstream/record');
const EventChannel('com.yourcompany.micstream/record');
class MicEvent {
final Uint16List audioData;
MicEvent(this.audioData);
Expand Down

0 comments on commit 5132420

Please sign in to comment.