forked from hungrymonkey/mic_stream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
committed files I changed. I messed up my directory. I have to rebuil…
…d it
- Loading branch information
1 parent
e5e5683
commit 5132420
Showing
7 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 55 additions & 1 deletion
56
example/android/app/src/main/java/com/yourcompany/micstreamexample/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters