如何使用 MediaController 添加播放视频的按钮?
2012 年 2 月 18 日更新,
我收到了忙碌的教练关于按钮应该是什么的回复,他说应该是 MediaController 按钮(播放、上一个和下一个),我相信它应该始终显示在屏幕上,并且不是自定义按钮。我认为“播放”按钮没有 onclicklistener。是否有用于单击“播放”按钮的侦听器(除了我的代码示例中的 onCreate 活动)。我试图不必使用意图来开始另一项活动。谢谢!
我的作业项目是修改现有项目(使用 MediaPlayer 和 MediaRecorder 类捕获音频),添加一个按钮(我假设这是我必须创建的东西,而不是显示 MediaController 时的“播放”按钮),以便在使用 MediaController 单击时播放视频。我尝试这样做,但我添加的代码无法播放视频。我的课堂材料中的示例使用 MediaController 的播放按钮,因此我想先了解如何使用自定义按钮来播放视频。然后再处理将其集成到现有项目中。请向我指出现有的示例代码或指导我进行这项工作。谢谢!
今天,我继续创建了一个单独的项目,该项目只有一个使用 MediaController 播放视频的按钮。正如预期的那样,它仍然无法工作(无法启动视频,NullPointerException)。我在下面展示了项目文件。我现在一无所知。请指出一两件事让我开始解决问题。再次感谢!
package com.mypackage;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.content.Context;
import android.os.Environment;
public class MediaActivity extends Activity {
private String path;
J
private VideoView vd;
//private Context context;
private String TAG = " ";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//context = this;
Button playVideoBtn =(Button)findViewById(R.id.playVideo);
playVideoBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
playVideo();
} catch (Exception ex) {
Log.e(TAG, "Failed to Start Playing the video", ex);
}
}
});
}
private void playVideo() throws Exception {
vd = (VideoView) findViewById(R.id.surface_view);
File directoryPath = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
directoryPath.mkdirs();
path = directoryPath.toString() + "/Familyguy_Has_Own_Orbit.3gp";
if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(MediaActivity.this, "Please edit MediaActivity, and set path"
+ " variable to your media file URL/path", Toast.LENGTH_LONG).show();
} else {
vd.setVideoPath(path);
vd.setMediaController(new MediaController(this));
vd.requestFocus();
vd.start();
}
}
}
这是我的布局文件:
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/playVideo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Playing Video"/>
</LinearLayout>
Videoview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<VideoView
android:id="@+id/surface_view"
android:layout_width="320px"
android:layout_height="240px"
/>
</LinearLayout>
Update on 2/18/2012
I received a reply from my busy instructor on what the button should be and he said it should be the MediaController buttons (Play, Previous, and Next) and I believe it should be shown always on the screen and not a custom button. I don't think there is an onclicklistener for the Play button. Is there a listener for the Play button click (other than onCreate activity in my code examples). I am trying not to have to use intent to start another activity. Thanks!
My homework project is to modify an existing project (which capture audio using MediaPlayer and MediaRecorder classes) by adding a button (I assume this is something I have to create and not the Play button when MediaController gets displayed) to play video when clicked using MediaController. I attempted to do this but the code I added fail to play video. The examples in my class material uses MediaController's Play button, so I would like to learn how to have a custom made button to play video first. Then deal with integrating it into the existing project later. Please point me to existing sample code or guide me in this endeavor. Thanks!
Today, I went ahead and created a separate project that only have a button to play video using MediaController. As expected, it still does not work (Failed to start video, NullPointerException). I present project files below. I am clueless right now. Please point out a thing or two to get me started solving the problem. Thanks again!
package com.mypackage;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.content.Context;
import android.os.Environment;
public class MediaActivity extends Activity {
private String path;
J
private VideoView vd;
//private Context context;
private String TAG = " ";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//context = this;
Button playVideoBtn =(Button)findViewById(R.id.playVideo);
playVideoBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try {
playVideo();
} catch (Exception ex) {
Log.e(TAG, "Failed to Start Playing the video", ex);
}
}
});
}
private void playVideo() throws Exception {
vd = (VideoView) findViewById(R.id.surface_view);
File directoryPath = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
directoryPath.mkdirs();
path = directoryPath.toString() + "/Familyguy_Has_Own_Orbit.3gp";
if (path == "") {
// Tell the user to provide a media file URL/path.
Toast.makeText(MediaActivity.this, "Please edit MediaActivity, and set path"
+ " variable to your media file URL/path", Toast.LENGTH_LONG).show();
} else {
vd.setVideoPath(path);
vd.setMediaController(new MediaController(this));
vd.requestFocus();
vd.start();
}
}
}
Here are my layout files:
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/playVideo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Playing Video"/>
</LinearLayout>
Videoview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<VideoView
android:id="@+id/surface_view"
android:layout_width="320px"
android:layout_height="240px"
/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想看看这些:
https://github.com/commonsguy/vidtry/tree
Android:如何创建视频播放器?
You might wanna take a look at these:
https://github.com/commonsguy/vidtry/tree
Android: How to create video player?