如何捕获视频或音频并仅在Android上显示? [英] How do I capture video or audio and display only on android ?
本文介绍了如何捕获视频或音频并仅在Android上显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经成功创建了用于捕获图像并在屏幕上显示图像,但请同样的方式来附加音频和放大器。视频也是如此,如何从按钮上的Gallery中选择附加媒体文件。
我尝试过:
I have successfully created for just capturing the Image and Displaying the Image on the Screen, But Please me the same way for Attaching Audio & Video too, And how do I get option of Attaching media file from Gallery onClick of Buttons.
What I have tried:
package inducesmile.com.androidcameraapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.VideoView;
public class MainActivity extends ActionBarActivity {
private ImageView imageHolder;
private VideoView videoHolder;
private final int requestCode = 20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageHolder = (ImageView)findViewById(R.id.captured_photo);
Button capturedImageButton = (Button)findViewById(R.id.photo_button);
Button capturedVideoButton = (Button)findViewById(R.id.Video_button);
Button capturedAudioButton = (Button)findViewById(R.id.Audio_button);
capturedImageButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoCaptureIntent, requestCode);
}
});
capturedVideoButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent videoCaptureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(videoCaptureIntent, requestCode);
}
});
capturedAudioButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent audioCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
startActivityForResult(audioCaptureIntent, requestCode);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK){
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
imageHolder.setImageBitmap(bitmap);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
推荐答案
我建议为每个<$ c的调用使用单独的请求代码$ C> startActivityForResult()。否则,在onActivityResult()
中无法区分它们。
例如,如果你是想要在VideoView
对象中显示捕获的视频的第一帧,只需调用其seekTo(0)
方法。
另一个想法是给它分配一个缩略图,例如:
I would suggest using separate request codes for each of the calls tostartActivityForResult()
. Otherwise, there's no way to distinguish between them inonActivityResult()
.
If, for example, you are wanting to show the first frame of the captured video in theVideoView
object, just call itsseekTo(0)
method.
Another idea would be to assign a thumbnail to it, something like:
Bitmap bmThumb = ThumbnailUtils.createVideoThumbnail("path", MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable bmDrawable = new BitmapDrawable(bmThumb);
videoHolder.setBackgroundDrawable(bmDrawable);
就音频而言,没有什么可以显示的。因此,即使您使用Voice Recorder应用程序捕获了某些内容,它也只是音频 - 没有图像。
As far as audio goes, there's nothing to display. So even if you captured something using the Voice Recorder app, it's just audio -- no image(s).
这篇关于如何捕获视频或音频并仅在Android上显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文