我正在尝试开发一个Android应用程序,其中我想使用来自json的url向用户显示电影预告片。该URL可以来自Youtube dailymotion或来自任何其他来源。请帮助我如何制作可以在我的应用程序中播放任何视频的视频播放器。 谢谢。
答案 0 :(得分:0)
借助MediaController和VideoView类,我们可以在android中播放视频文件。
示例 - 强>
activity_main.xml -
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView =(VideoView)findViewById(R.id.videoView1);
//Creating MediaController
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
//specify the location of media file
Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/media/1.mp4");
//Setting MediaController and URI, then starting the videoView
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MainActivity.java -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
答案 1 :(得分:0)
在Xml中定义VideoView
videoview = (VideoView) findViewById(R.id.VideoView);
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
VideoViewActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
}
});
之后在您的活动中定义此代码
ViedoView videoview;
import mysql.connector
import sys
import time
import paramiko
host = 'remote-ssh-host'
i = 1
while True:
print("Trying to connect to %s (%i/30)" % (host, i))
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port=22, username='sshuser', password='sshpwd')
print("Connected to %s" % host)
break
except paramiko.AuthenticationException:
print("Authentication failed when connecting to %s" % host)
sys.exit(1)
except:
print("Could not SSH to %s, waiting for it to start" % host)
i += 1
time.sleep(2)
# If we could not connect within time limit
if i == 30:
print("Could not connect to %s. Giving up" % host)
sys.exit(1)
cnx = mysql.connector.connect(user="mysqluser", password="mysqlpwd",
host="mysqlhost",
port=3307)
这对我有用.. :))
答案 2 :(得分:0)
试试这个:
VideoView videoview;
videoview = (VideoView) findViewById(R.id.videodetail_view);
videoview.requestFocus();
试 {
final MediaController mediacontroller = new MediaController(Activity.this,true);
videoview.setMediaController(mediacontroller);
mediacontroller.setAnchorView(videoview);
// mediacontroller.canScrollHorizontally(0);
scroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
mediacontroller.hide();
}
});
Uri video = Uri.parse(videoUrl);
// videoview.setActivity(this);
// videoview.setShouldAutoplay(true);
videoview.setVideoURI(video);
} catch (Exception e) {
e.printStackTrace();
}
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
}
});
In XmlLayout
<VideoView android:id="@+id/videodetail_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
android:adjustViewBounds="true"
android:background="@drawable/noimage"
android:cropToPadding="true"
android:scaleType="fitXY"/>