我希望在单击按钮后在thextureView中播放视频,但它不起作用,而我的代码工作,如果我把它放在onCreate方法,如果我不在onCreate中创建它,我是不能播放它?
我希望在单击按钮后在thextureView中播放视频,但它不起作用,而我的代码工作,如果我把它放在onCreate方法,如果我不在onCreate中创建它,我是不能播放它?
我希望在单击按钮后在thextureView中播放视频,但它不起作用,而我的代码工作,如果我把它放在onCreate方法,如果我不在onCreate中创建它,我是不能播放它? 这是我的代码:
public class TestActivity extends AppCompatActivity {
private TextureView mPreview;
private MediaPlayer mMediaPlayer;
private TextureView.SurfaceTextureListener surfaceTextureListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mPreview = (TextureView) findViewById(R.id.surfaceView);
//I COMMENT THIS PART ANDE RUN IT IN onClick BUT ITS NOT WORK :/
// surfaceTextureListener=new TextureView.SurfaceTextureListener() {
// @Override
// public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
// Surface s = new Surface(surface);
// try {
// mMediaPlayer = new MediaPlayer();
// mMediaPlayer.setDataSource("/storage/emulated/0/dff91403790b4f1702f4cda4b83626fa.mp4");
// mMediaPlayer.setSurface(s);
// mMediaPlayer.setLooping(true);
// mMediaPlayer.prepare();
...
// mPreview.setSurfaceTextureListener(surfaceTextureListener);
//
}
//EDITED
public void onClick(View v){
//THIS PART NOT WORK
surfaceTextureListener=new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource("/storage/emulated/0/dff91403790b4f1702f4cda4b83626fa.mp4");
mMediaPlayer.setSurface(s);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
mPreview.setSurfaceTextureListener(surfaceTextureListener);
}
}
请帮助我?
答案 0 :(得分:0)
您是否在xml属性中设置onClick?如果是,则发生这种情况,因为您已使用onclick属性在xml中声明了该方法。当您使用xml中的onclick属性声明时,您必须在java代码中使用public修饰符才能工作。 替换你的代码
void onClick(View v)
以下行
public void onClick(View v){
希望能帮到你。