我已尝试过所有内容,但我无法使用VideoView类播放视频。这是我的代码:
String fileName = "android.resource://"+ getPackageName() +"/raw/test";
VideoView vv = (VideoView) this.findViewById(R.id.videoView);
vv.setVideoURI(Uri.parse(fileName));
vv.start();
视频采用mp4格式,存在于raw
文件夹中。
该文件的链接是否错误?
我会很高兴得到任何帮助。
答案 0 :(得分:2)
我建议您使用以下代码,其中我正在成功运行我的应用程序
守则如下:
XML文件:
<Button
android:id="@+id/btnVideoGallery"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="@string/gallery" />
<Button
android:id="@+id/btnCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/btnVideoGallery"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="@string/cancel" />
<TextView
android:id="@+id/lblDisplayImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCancel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/below_this_text_video_will_be_displayed"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="13dp" />
<VideoView
android:id="@+id/vvDisplayVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lblDisplayImage"
android:layout_marginTop="15dp" />
Java文件:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoActivity extends Activity {
private Button btnVideoGallery,btnCancel;
private VideoView vvDisplayVideo;
/** The Constant PICK_VIDEO. */
private static final int PICK_VIDEO=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_options);
btnVideoGallery=(Button)findViewById(R.id.btnVideoGallery);
vvDisplayVideo=(VideoView)findViewById(R.id.vvDisplayVideo);
btnCancel=(Button)findViewById(R.id.btnCancel);
vvDisplayVideo.setVisibility(View.GONE);
btnVideoGallery.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent video=new Intent();
video.setAction(Intent.ACTION_PICK);
video.setType("video/*");
startActivityForResult(video, PICK_VIDEO);
}
});
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goStartUp);
finish();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode==Activity.RESULT_OK && requestCode == PICK_VIDEO) {
vvDisplayVideo.setVisibility(View.VISIBLE);
vvDisplayVideo.setVideoURI(data.getData());
vvDisplayVideo.setFocusable(true);
MediaController mc=new MediaController(this);
vvDisplayVideo.setMediaController(mc);
Log.i("True", "Executed");
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goStartUp);
finish();
return super.onKeyDown(keyCode, event);
}
}
您也可以根据您的使用修改清单文件:
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<application .....
</application>
</manifest>
答案 1 :(得分:0)
我的代码中没有发现任何明显错误。
以下是我将采取的措施来跟踪此错误:
使用这些新信息更新您的帖子
祝你好运!答案 2 :(得分:0)
这个简单的代码对我有用: (原始文件夹中的my_video.mp4和VideoView @ + id / splash)
VideoView mVideoView = (VideoView) findViewById(R.id.splash);
Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.my_video);
mVideoView.setVideoURI(videoUri);
mVideoView.start();