如何将 MediaController 放入 TextureView
How to put a MediaController in a TextureView
我正在尝试将媒体控制器放入我的纹理视图中,但我总是遇到错误
E/MediaPlayer﹕ Should have subtitle controller already set
这是我的代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.texture_video);
textureView = (TextureView) findViewById(R.id.textureView);
listView = (ListView) findViewById(R.id.list);
.
.
.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
onDestroy();
FILE_NAME = "test1.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 1) {
onDestroy();
FILE_NAME = "test2.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 2) {
onDestroy();
FILE_NAME = "test3.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 3) {
onDestroy();
FILE_NAME = "test4.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
}
}
});
textureView.setSurfaceTextureListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
onSurfaceTextureUpdated(surfaceTexture);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// set up media controller
videoController = new MediaController(VideoAssetActivity.this);
videoController.setAnchorView(textureView);
videoController.setPadding(0, 0, 0, 0);
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
我的问题是我已经设置了控制器,但即使我播放其他 mp4 也没有显示。我已经搜索了解决方案,但我只看到了音频文件。
谁能帮我?非常感谢..
- 实施
MediaController.MediaPlayerControl
- 覆盖需要的函数
通过此代码显示控制器:
MediaController videoController = new MediaController(getActivity());
videoController.setMediaPlayer(this);//your activity which implemented MediaPlayerControl
videoController.setAnchorView(mTextureView);
videoController.setEnabled(true);
videoController.show();
玩得开心!
我正在尝试将媒体控制器放入我的纹理视图中,但我总是遇到错误
E/MediaPlayer﹕ Should have subtitle controller already set
这是我的代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.texture_video);
textureView = (TextureView) findViewById(R.id.textureView);
listView = (ListView) findViewById(R.id.list);
.
.
.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
onDestroy();
FILE_NAME = "test1.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 1) {
onDestroy();
FILE_NAME = "test2.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 2) {
onDestroy();
FILE_NAME = "test3.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
} else if (position == 3) {
onDestroy();
FILE_NAME = "test4.mp4";
textureView.setSurfaceTextureListener(VideoAssetActivity.this);
onSurfaceTextureAvailable(textureView.getSurfaceTexture(), view.getWidth(), view.getHeight());
}
}
});
textureView.setSurfaceTextureListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
Surface surface = new Surface(surfaceTexture);
try {
AssetFileDescriptor afd = getAssets().openFd(FILE_NAME);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepareAsync();
onSurfaceTextureUpdated(surfaceTexture);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// set up media controller
videoController = new MediaController(VideoAssetActivity.this);
videoController.setAnchorView(textureView);
videoController.setPadding(0, 0, 0, 0);
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
我的问题是我已经设置了控制器,但即使我播放其他 mp4 也没有显示。我已经搜索了解决方案,但我只看到了音频文件。 谁能帮我?非常感谢..
- 实施
MediaController.MediaPlayerControl
- 覆盖需要的函数
通过此代码显示控制器:
MediaController videoController = new MediaController(getActivity()); videoController.setMediaPlayer(this);//your activity which implemented MediaPlayerControl videoController.setAnchorView(mTextureView); videoController.setEnabled(true); videoController.show();
玩得开心!