api: media module
This commit is contained in:
@@ -13,6 +13,7 @@ import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.stardust.autojs.annotation.ScriptInterface;
|
||||
import com.stardust.util.IntentUtil;
|
||||
import com.stardust.util.MimeTypes;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
@@ -99,8 +100,7 @@ public class AppUtils {
|
||||
if (path == null)
|
||||
throw new NullPointerException("path == null");
|
||||
path = "file://" + path;
|
||||
String ext = getExtension(path);
|
||||
String mimeType = TextUtils.isEmpty(ext) ? "*/*" : MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
|
||||
String mimeType = MimeTypes.fromFileOr(path, "*/*");
|
||||
mContext.startActivity(new Intent(Intent.ACTION_VIEW)
|
||||
.setDataAndType(Uri.parse(path), mimeType)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
||||
@@ -111,8 +111,7 @@ public class AppUtils {
|
||||
if (path == null)
|
||||
throw new NullPointerException("path == null");
|
||||
path = "file://" + path;
|
||||
String ext = getExtension(path);
|
||||
String mimeType = TextUtils.isEmpty(ext) ? "*/*" : MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
|
||||
String mimeType = MimeTypes.fromFileOr(path, "*/*");
|
||||
mContext.startActivity(new Intent(Intent.ACTION_EDIT)
|
||||
.setDataAndType(Uri.parse(path), mimeType)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
||||
|
||||
@@ -7,13 +7,16 @@ import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Matrix;
|
||||
import android.media.Image;
|
||||
import android.media.MediaScannerConnection;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Display;
|
||||
import android.view.Surface;
|
||||
import android.view.WindowManager;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.stardust.autojs.annotation.ScriptVariable;
|
||||
import com.stardust.autojs.core.image.ColorFinder;
|
||||
@@ -41,6 +44,10 @@ import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Locale;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
|
||||
import static com.stardust.pio.PFiles.getExtension;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/20.
|
||||
*/
|
||||
@@ -233,4 +240,8 @@ public class Images {
|
||||
}
|
||||
|
||||
|
||||
public void notityImageInserted(String path){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
156
autojs/src/main/java/com/stardust/autojs/runtime/api/Media.java
Normal file
156
autojs/src/main/java/com/stardust/autojs/runtime/api/Media.java
Normal file
@@ -0,0 +1,156 @@
|
||||
package com.stardust.autojs.runtime.api;
|
||||
|
||||
import android.content.Context;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.MediaScannerConnection;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
import com.stardust.util.MimeTypes;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/2/12.
|
||||
*/
|
||||
|
||||
public class Media implements MediaScannerConnection.MediaScannerConnectionClient {
|
||||
|
||||
private MediaScannerConnection mScannerConnection;
|
||||
private MediaPlayerWrapper mMediaPlayer;
|
||||
|
||||
public Media(Context context) {
|
||||
mScannerConnection = new MediaScannerConnection(context, this);
|
||||
}
|
||||
|
||||
public void scan(String path) {
|
||||
String mimeType = MimeTypes.fromFileOr(path, null);
|
||||
mScannerConnection.scanFile(path, mimeType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMediaScannerConnected() {
|
||||
|
||||
}
|
||||
|
||||
public void playMusic(String path, float volume, boolean looping) {
|
||||
if (mMediaPlayer == null) {
|
||||
mMediaPlayer = new MediaPlayerWrapper();
|
||||
}
|
||||
mMediaPlayer.stopAndReset();
|
||||
try {
|
||||
mMediaPlayer.setDataSource(path);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
mMediaPlayer.setVolume(volume, volume);
|
||||
mMediaPlayer.setLooping(looping);
|
||||
mMediaPlayer.start();
|
||||
}
|
||||
|
||||
public void pauseMusic() {
|
||||
if (mMediaPlayer == null)
|
||||
return;
|
||||
mMediaPlayer.pause();
|
||||
}
|
||||
|
||||
public void resumeMusic() {
|
||||
if (mMediaPlayer == null)
|
||||
return;
|
||||
mMediaPlayer.start();
|
||||
}
|
||||
|
||||
public void stopMusic() {
|
||||
if (mMediaPlayer == null)
|
||||
return;
|
||||
mMediaPlayer.stop();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onScanCompleted(String path, Uri uri) {
|
||||
|
||||
}
|
||||
|
||||
public void recycle() {
|
||||
if (mScannerConnection != null) {
|
||||
mScannerConnection.disconnect();
|
||||
mScannerConnection = null;
|
||||
}
|
||||
if (mMediaPlayer != null) {
|
||||
mMediaPlayer.release();
|
||||
}
|
||||
}
|
||||
|
||||
private static class MediaPlayerWrapper extends MediaPlayer {
|
||||
|
||||
static final int STATE_NOT_INITIALIZED = 0;
|
||||
static final int STATE_PREPARING = 1;
|
||||
static final int STATE_PREPARED = 2;
|
||||
static final int STATE_START = 3;
|
||||
static final int STATE_PAUSED = 4;
|
||||
static final int STATE_STOPPED = 5;
|
||||
static final int STATE_RELEASED = 6;
|
||||
|
||||
private int mState = STATE_NOT_INITIALIZED;
|
||||
|
||||
public int getState() {
|
||||
return mState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepare() throws IOException, IllegalStateException {
|
||||
mState = STATE_PREPARING;
|
||||
super.prepare();
|
||||
mState = STATE_PREPARED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareAsync() throws IllegalStateException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws IllegalStateException {
|
||||
super.start();
|
||||
mState = STATE_START;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws IllegalStateException {
|
||||
super.stop();
|
||||
mState = STATE_STOPPED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() throws IllegalStateException {
|
||||
super.pause();
|
||||
mState = STATE_PAUSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release() {
|
||||
super.release();
|
||||
mState = STATE_RELEASED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
super.reset();
|
||||
mState = STATE_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
public void stopAndReset() {
|
||||
try {
|
||||
if (mState == STATE_START || mState == STATE_PAUSED) {
|
||||
stop();
|
||||
}
|
||||
if (mState != STATE_NOT_INITIALIZED) {
|
||||
reset();
|
||||
}
|
||||
} catch (IllegalStateException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user