gpt4 book ai didi

android - 当互联网不存在时播放视频

转载 作者:行者123 更新时间:2023-11-30 02:26:04 25 4
gpt4 key购买 nike

我正在开发关于 VideoPlayer 的 android 应用程序。我在互联网上播放视频,但我想在 Android 设备未连接到互联网时播放。我认为首先我将来自 intrnet 的视频安装到应用程序中,如果设备上存在互联网,它会从互联网播放,但是当互联网不存在时,它应该在应用程序中播放我该怎么做?我的代码在下面

公共(public)类 AndroidVideoPlayer 扩展 Activity 实现 SurfaceHolder.Callback, OnClickListener {

WakeLock wakeLock;

MediaPlayer mediaPlayer;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean pausing = false;;

Timer timer;

int maxVolume = 50;

int userInteractionTimeout;

Button sound;

int currentVolume;

String url = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";

/** Called when the activity is first created. */

ConnectivityManager cm;

NetworkInfo activeNetwork;
boolean isConnected;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

try {
// REQUIRES ROOT

Build.VERSION_CODES vc = new Build.VERSION_CODES();
Build.VERSION vr = new Build.VERSION();
String ProcID = "79"; // HONEYCOMB AND OLDER

// v.RELEASE //4.0.3
if (vr.SDK_INT >= vc.ICE_CREAM_SANDWICH) {
ProcID = "42"; // ICS AND NEWER
}

// REQUIRES ROOT
Process proc = Runtime.getRuntime().exec(
new String[] {
"su",
"-c",
"service call activity " + ProcID
+ " s16 com.android.systemui" }); // WAS
// 79
proc.waitFor();

} catch (Exception ex) {
// Toast.LENGTH_LONG).show();
}

setContentView(R.layout.main);

cm = (ConnectivityManager) getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);

activeNetwork = cm.getActiveNetworkInfo();
isConnected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
Log.i("@mle", "" + isConnected);

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"Full Wake Lock");

sound = (Button) findViewById(R.id.btn_volume);
sound.setOnClickListener(this);
sound.setVisibility(View.VISIBLE);
sound.setBackgroundColor(Color.TRANSPARENT);

// AudioManager audio = (AudioManager)
// getSystemService(Context.AUDIO_SERVICE);
// currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);

/*
* timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() {
*
* @Override public void run() { if (userInteractionTimeout == 15) { //
* Do your @Override
*
* timer.cancel(); Intent intent = new Intent(getApplicationContext(),
* MainActivity.class); intent.putExtra("EXIT", false);
* intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
* startActivity(intent);
*
* } userInteractionTimeout++; } }, 0, 1000);
*/

getWindow().setFormat(PixelFormat.UNKNOWN);

// Displays a video file.
VideoView mVideoView = (VideoView) findViewById(R.id.videoview);

// String uriPath = "android.resource://com.example.anket1/"
// + R.raw.ucz_video;
// Uri uri = Uri.parse(uriPath);
// mVideoView.setVideoURI(uri);

// mVideoView.requestFocus();

if (isConnected == true) {

DownloadFromUrl(url, "file");

/*
* mVideoView.setVideoPath(url);
*
* mVideoView.start();
*
* // video finish listener mVideoView .setOnCompletionListener(new
* MediaPlayer.OnCompletionListener() {
*
* @Override public void onCompletion(MediaPlayer mp) { // not
* playVideo // playVideo();
*
* mp.start(); } });
*/

}

else {

String uriPath = "android.resource://com.example.anket1/"
+ R.raw.ucz_video;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.start();

// video finish listener
mVideoView
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
// not playVideo
// playVideo();

mp.start();
}
});
}

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
wakeLock.acquire();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wakeLock.release();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();

switch (eventaction) {
case MotionEvent.ACTION_DOWN:

Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
intent.putExtra("EXIT", false);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);

break;

/*
* case MotionEvent.ACTION_MOVE: // finger moves on the screen break;
*
* case MotionEvent.ACTION_UP: // finger leaves the screen break;
*/
}

// tell the system that we handled the event and no further processing
// is required
return true;
}

public String DownloadFromUrl(String DownloadUrl, String fileName) {

File SDCardRoot = null;
try {
SDCardRoot = Environment.getDataDirectory();
File files = new File(SDCardRoot + fileName);
int sizeoffile;
if (!files.exists()) {
File root = android.os.Environment.getDataDirectory();

File dir = new File(root.getAbsolutePath());
if (dir.exists() == false) {
dir.mkdirs();
}

URL url = new URL(DownloadUrl);
File file = new File(dir, fileName);

/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
sizeoffile = ucon.getContentLength();
Log.d("SIZEOFFILE: ", sizeoffile + " BYTE");
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

/*
* Read bytes to the Buffer until there is nothing more to
* read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
}
} catch (IOException e) {
e.getMessage();
}

return SDCardRoot + fileName;
}

/*
* @Override public void onUserInteraction() { // TODO Auto-generated method
* stub super.onUserInteraction();
*
* userInteractionTimeout = 0;
*
* // Log.d(LOG_TAG,"User Interaction : "+userInteractionTimeout);
*
* }
*/

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

switch (v.getId()) {
case R.id.btn_volume:

Intent pas = new Intent(getApplicationContext(),
SetVolumeLevel.class);
startActivity(pas);

break;

default:
break;
}

}

最佳答案

如果网络可用然后首先下载视频,您可以使用以下代码:

public String DownloadFromUrl(String DownloadUrl, String fileName) {

File SDCardRoot = null;
try {
SDCardRoot = Environment.getExternalStorageDirectory();
File files = new File(SDCardRoot+fileName);
int sizeoffile;
if(!files.exists())
{
File root = android.os.Environment.getExternalStorageDirectory();

File dir = new File (root.getAbsolutePath());
if(dir.exists()==false) {
dir.mkdirs();
}

URL url = new URL(DownloadUrl);
File file = new File(dir, fileName);

/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
sizeoffile = ucon.getContentLength();
Log.d("SIZEOFFILE: ", sizeoffile+" BYTE");
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
}
}
catch (IOException e) {
e.getMessage();
}

return SDCardRoot+fileName;
}

然后只需检查互联网是否可用,然后通过互联网播放,否则播放您 sd 上已有的视频。

关于android - 当互联网不存在时播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27898838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com