Java Android:如何从 URL 播放 mp4 视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21849602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: how to play mp4 video from URL?
提问by spacitron
I'm trying to play video but having no luck so I'm testing with some bare bones code to see what the problem is. The following doesn't work and I'm not really sure why:
我正在尝试播放视频但没有运气,所以我正在使用一些基本代码进行测试以查看问题所在。以下不起作用,我不确定为什么:
Manifest:
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videotest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.videotest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity:
主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView vid = (VideoView) findViewById(R.id.videoView1);
Uri vidUri = Uri.parse("http://somewebsite.com/somevideo.mp4");
vid.setVideoURI(vidUri);
vid.setMediaController(new MediaController(this));
vid.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main:
活动_主要:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
This is the error I am given when running the code:
这是我运行代码时出现的错误:
02-18 10:12:44.071: E/MediaPlayer(8357): java.io.FileNotFoundException: No content provider: http://somewebsite.com/somevideo.mp4
回答by Jigar Shekh
Your Uri in not genuine. Try this Uri. Check this link http://androidcodeexamples.blogspot.in/2011/08/how-to-play-mp4-video-in-android-using.html
你的 Uri 不是真的。试试这个 Uri。检查此链接http://androidcodeexamples.blogspot.in/2011/08/how-to-play-mp4-video-in-android-using.html
Uri vidUri = Uri.parse("http://s1133.photobucket.com/user/Anniebabycupcakez/media/1489553974996_37622.mp4.html");
回答by Rollyng
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(""http://somewebsite.com/somevideo.mp4"");
}
or with VideoView don't forget requestfocus
或使用 VideoView 不要忘记 requestfocus
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse("http://somewebsite.com/somevideo.mp4"));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
回答by Seraphim's
Sample code:
示例代码:
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.videodisplay);
String link="http://s1133.photobucket.com/albums/m590/Anniebabycupcakez/?action=view& current=1376992942447_242.mp4";
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(link);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
}
}
回答by eimmer
I think you are calling vid.start() before the VideoView is in the correct state, try this:
我认为您在 VideoView 处于正确状态之前调用了 vid.start(),试试这个:
vid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
vid.start();
}
});
vid.setVideoURI(videoUri);
You can also register a listener with setOnErrorListener() that might give you more info on why it's failing.
您还可以使用 setOnErrorListener() 注册一个侦听器,这可能会为您提供有关失败原因的更多信息。
回答by Makvin
@Override
public void surfaceCreated(SurfaceHolder holder) {
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.abc);
try {
mp.setDataSource(this, video);
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
mp.setDisplay(holder);
mp.start();
or get video from url just pass your url into Uri.parse() method
或从 url 获取视频只需将您的 url 传递到 Uri.parse() 方法