-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from faku99/master
Basic media controls implementation.
- Loading branch information
Showing
10 changed files
with
319 additions
and
105 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
104 changes: 99 additions & 5 deletions
104
src/android/src/main/java/com/twofind/stereo/StereoPlugin.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,29 +1,123 @@ | ||
package com.twofind.stereo; | ||
|
||
import android.media.MediaPlayer; | ||
import android.net.Uri; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | ||
import io.flutter.plugin.common.MethodChannel.Result; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.PluginRegistry.Registrar; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* StereoPlugin | ||
*/ | ||
public class StereoPlugin implements MethodCallHandler { | ||
private MediaPlayer mediaPlayer; | ||
|
||
/** | ||
* Plugin registration. | ||
*/ | ||
public static void registerWith(Registrar registrar) { | ||
final MethodChannel channel = new MethodChannel(registrar.messenger(), "stereo"); | ||
final MethodChannel channel = new MethodChannel(registrar.messenger(), "com.twofind.stereo"); | ||
channel.setMethodCallHandler(new StereoPlugin()); | ||
} | ||
|
||
@Override | ||
public void onMethodCall(MethodCall call, Result result) { | ||
if (call.method.equals("getPlatformVersion")) { | ||
result.success("Android " + android.os.Build.VERSION.RELEASE); | ||
} else { | ||
// isPlaying() method. | ||
if (call.method.equals("app.isPlaying")) { | ||
result.success(isPlaying()); | ||
} | ||
// load() method. | ||
else if (call.method.equals("app.load")) { | ||
if (call.arguments != null) { | ||
if (!(call.arguments instanceof String)) { | ||
result.error("WRONG_FORMAT", "The specified URL must be a string.", null); | ||
} | ||
|
||
String path = (String)call.arguments; | ||
|
||
result.success(load(path)); | ||
} else { | ||
result.error("NO_URL", "No URL was specified.", null); | ||
} | ||
} | ||
// pause() method. | ||
else if (call.method.equals("app.pause")) { | ||
pause(); | ||
|
||
result.success(null); | ||
} | ||
// play() method. | ||
else if (call.method.equals("app.play")) { | ||
play(); | ||
|
||
result.success(null); | ||
} | ||
// stop() method. | ||
else if (call.method.equals("app.stop")) { | ||
stop(); | ||
|
||
result.success(null); | ||
} | ||
// Method not implemented. | ||
else { | ||
result.notImplemented(); | ||
} | ||
} | ||
|
||
private boolean isPlaying() { | ||
if (mediaPlayer != null) { | ||
return mediaPlayer.isPlaying(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private int load(String path) { | ||
if (mediaPlayer != null) { | ||
mediaPlayer.stop(); | ||
mediaPlayer.release(); | ||
mediaPlayer = null; | ||
} | ||
|
||
mediaPlayer = new MediaPlayer(); | ||
|
||
try { | ||
mediaPlayer.setDataSource(path); | ||
} catch (IOException e) { | ||
return 1; | ||
} | ||
|
||
try { | ||
mediaPlayer.prepare(); | ||
} catch (IOException e) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private void pause() { | ||
if (mediaPlayer != null) { | ||
mediaPlayer.pause(); | ||
} | ||
} | ||
|
||
private void play() { | ||
if (mediaPlayer != null) { | ||
mediaPlayer.start(); | ||
} | ||
} | ||
|
||
private void stop() { | ||
if (mediaPlayer != null) { | ||
mediaPlayer.stop(); | ||
mediaPlayer.release(); | ||
|
||
mediaPlayer = null; | ||
} | ||
} | ||
} |
Oops, something went wrong.