提交 858764c4 编写于 作者: 董重

视频播放悬浮窗

上级 e597c158
...@@ -25,6 +25,4 @@ dependencies { ...@@ -25,6 +25,4 @@ dependencies {
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.dongzhong:VideoPlayer:v1.2'
} }
...@@ -3,6 +3,9 @@ package dongzhong.testforfloatingwindow; ...@@ -3,6 +3,9 @@ package dongzhong.testforfloatingwindow;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.provider.Settings; import android.provider.Settings;
...@@ -10,10 +13,13 @@ import android.support.annotation.Nullable; ...@@ -10,10 +13,13 @@ import android.support.annotation.Nullable;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Toast;
import dongzhong.videoplayer.VideoPlayer; import java.io.IOException;
/** /**
* Created by admin on 2018/5/30. * Created by admin on 2018/5/30.
...@@ -25,6 +31,7 @@ public class FloatingVideoService extends Service { ...@@ -25,6 +31,7 @@ public class FloatingVideoService extends Service {
private WindowManager windowManager; private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams; private WindowManager.LayoutParams layoutParams;
private MediaPlayer mediaPlayer;
private View displayView; private View displayView;
@Override @Override
...@@ -45,6 +52,8 @@ public class FloatingVideoService extends Service { ...@@ -45,6 +52,8 @@ public class FloatingVideoService extends Service {
layoutParams.height = 450; layoutParams.height = 450;
layoutParams.x = 300; layoutParams.x = 300;
layoutParams.y = 300; layoutParams.y = 300;
mediaPlayer = new MediaPlayer();
} }
@Nullable @Nullable
...@@ -63,12 +72,40 @@ public class FloatingVideoService extends Service { ...@@ -63,12 +72,40 @@ public class FloatingVideoService extends Service {
if (Settings.canDrawOverlays(this)) { if (Settings.canDrawOverlays(this)) {
LayoutInflater layoutInflater = LayoutInflater.from(this); LayoutInflater layoutInflater = LayoutInflater.from(this);
displayView = layoutInflater.inflate(R.layout.video_display, null); displayView = layoutInflater.inflate(R.layout.video_display, null);
VideoPlayer videoPlayer = displayView.findViewById(R.id.videoplayer_display);
videoPlayer.preset("https://raw.githubusercontent.com/dongzhong/ImageAndVideoStore/master/Bruno%20Mars%20-%20Treasure.mp4",
"Treasure - Bruno Mars", true);
windowManager.addView(displayView, layoutParams);
displayView.setOnTouchListener(new FloatingOnTouchListener()); displayView.setOnTouchListener(new FloatingOnTouchListener());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
SurfaceView surfaceView = displayView.findViewById(R.id.video_display_surfaceview);
final SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(surfaceHolder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
try {
mediaPlayer.setDataSource(this, Uri.parse("https://raw.githubusercontent.com/dongzhong/ImageAndVideoStore/master/Bruno%20Mars%20-%20Treasure.mp4"));
mediaPlayer.prepareAsync();
}
catch (IOException e) {
Toast.makeText(this, "无法打开视频源", Toast.LENGTH_LONG).show();
}
windowManager.addView(displayView, layoutParams);
} }
} }
...@@ -97,7 +134,7 @@ public class FloatingVideoService extends Service { ...@@ -97,7 +134,7 @@ public class FloatingVideoService extends Service {
default: default:
break; break;
} }
return false; return true;
} }
} }
} }
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<dongzhong.videoplayer.VideoPlayer <SurfaceView
android:id="@+id/videoplayer_display" android:id="@+id/video_display_surfaceview"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
</FrameLayout> </LinearLayout>
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册