- 기본코드
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
View itemView = activity.getLayoutInflater().inflate(R.layout.dialog_view, null);
itemView.findViewById(R.id.actionClose).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setView(itemView);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
다이얼로그를 생성한뒤 setView를 한다.
다이얼로그의 FEATURE_NO_TITLE를 지정하고 다이얼로그 윈도우를 투명하게 변경한다.
2019년 5월 23일 목요일
2019년 5월 15일 수요일
안드로이드 FileProvider 예제
- FileProvider를 통해 앱 내부 저장소에 저장된 파일을 외부로 공유한다.
1. 기본코드
- res/xml/file_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
name="image"
path="temp_image/" />
<files-path
name="text"
path="temp_text/" />
</paths>
image, text 이름은 실제로 temp_image, temp_text 디렉토리명으로 연결된다.
files-path를 적용했으므로 디렉토리 경로는
/data/user/0/simple.app.simplefileprovider/files/temp_image/
/data/user/0/simple.app.simplefileprovider/files/temp_text/
- AndroidManifest.xml
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="simple.app.simplefileprovider.fileprovider"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
android:authorities 값은 패키지명에 fileprovider를 붙이는게 일반적이다.
- Intent 파일 Uri 전달.
File dir = new File(getFilesDir(), "temp_image");
File file = new File(dir, "image.jpg");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(FileProvider.getUriForFile(getBaseContext(), AUTHORITY, file), "image/*");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
FileProvider를 사용해서 Uri를 가져온다.
여기서 사용되는 Uri주소는
content://simple.app.simplefileprovider.fileprovider/image/image.jpg
FLAG_GRANT_READ_URI_PERMISSION를 사용해야 한다.
안드로이드 N 버전 부터 반드시 적용해야 하는 사양이다.
2. 샘플코드
소스코드
APK
1. 기본코드
- res/xml/file_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
name="image"
path="temp_image/" />
<files-path
name="text"
path="temp_text/" />
</paths>
image, text 이름은 실제로 temp_image, temp_text 디렉토리명으로 연결된다.
files-path를 적용했으므로 디렉토리 경로는
/data/user/0/simple.app.simplefileprovider/files/temp_image/
/data/user/0/simple.app.simplefileprovider/files/temp_text/
- AndroidManifest.xml
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="simple.app.simplefileprovider.fileprovider"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
android:authorities 값은 패키지명에 fileprovider를 붙이는게 일반적이다.
- Intent 파일 Uri 전달.
File dir = new File(getFilesDir(), "temp_image");
File file = new File(dir, "image.jpg");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(FileProvider.getUriForFile(getBaseContext(), AUTHORITY, file), "image/*");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
FileProvider를 사용해서 Uri를 가져온다.
여기서 사용되는 Uri주소는
content://simple.app.simplefileprovider.fileprovider/image/image.jpg
FLAG_GRANT_READ_URI_PERMISSION를 사용해야 한다.
안드로이드 N 버전 부터 반드시 적용해야 하는 사양이다.
2. 샘플코드
소스코드
APK
라벨:
안드로이드,
FileProvider
위치:
대한민국
2019년 5월 14일 화요일
안드로이드 Fragment Tab 예제
- Fragment를 활용한 탭 예제
0. 탭 화면을 구성할 때 주의사항.
네비게이션 흐름상 ListFragment에서 ListItemFragment로 이동해야 하는 경우
getSupportFragmentManager().beginTransaction().replace(R.id.tab_container, ListItemFragment.newInstance()).commit();
형태로 사용하는 경우가 종종 있지만
Tab 내에서 UI 이동이 발생하면 Fragment 흐름을 관리하기 어렵다.
Tab을 사용할 때는 Activity로 List 항목을 표현하는 것을 추천.
1. 탭 구성
Tab1Fragment, Tab2Fragment, Tab3Fragment를 생성한 뒤
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.findFragmentById(R.id.tab_container) == null) {
Tab1Fragment tab1 = Tab1Fragment.newInstance("Home Tab");
Tab2Fragment tab2 = Tab2Fragment.newInstance("Dash Board Tab");
Tab3Fragment tab3 = Tab3Fragment.newInstance("Notification Tab");
fragmentManager.beginTransaction()
.add(R.id.tab_container, tab1, tab1.getClass().getSimpleName()).show(tab1)
.add(R.id.tab_container, tab2, tab2.getClass().getSimpleName()).hide(tab2)
.add(R.id.tab_container, tab3, tab3.getClass().getSimpleName()).hide(tab3).commit();
}
첫번째 탭만 show를 하고 나머지 탭은 모두 hide 처리
Activity의 onCreate에서 위 코드를 적용하면 FragmentManager에 적용된 순서와 동일하게
onCreate > onCreateView > onResume
상태가 된다.
2. Tab 이동
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
List<Fragment> fragments = fragmentManager.getFragments();
for (Fragment item : fragments) {
if (item.getClass().getSimpleName().equals(tabName)) {
beginTransaction.show(item);
} else {
beginTransaction.hide(item);
}
}
beginTransaction.commit();
getFragments 메소드를 통해서 현재 탭을 모두 가져와서 보여질 Fragment만 show 처리하고 나머지 Fragment는 모두 hide 처리한다.
3. 탭 데이터 갱신
동일한 탭 또는 다른탭을 선택 했을 때 Fragment 내용을 변경해야 한다면 인터페이스를 적용해서 데이터를 갱신한다.
- 인터페이스
public interface OnDataUpdate {
/**
* 데이터 갱신
*/
void onUpdateData();
}
- 인터페이스 구현
public class Tab1Fragment extends Fragment implements OnDataUpdate {
@Override
public void onUpdateData() {
Log.d("Fragment", text + " : onUpdateData");
count++;
TextView textView = getView().findViewById(R.id.text);
textView.setText(String.format("%s %d", text, count));
}
}
- 데이터 갱신
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
List<Fragment> fragments = fragmentManager.getFragments();
for (Fragment item : fragments) {
if (item.getClass().getSimpleName().equals(tabName)) {
beginTransaction.show(item);
/*보여질 탭 갱신*/
if (item instanceof OnDataUpdate)
((OnDataUpdate) item).onUpdateData();
} else {
beginTransaction.hide(item);
}
}
beginTransaction.commit();
OnDataUpdate 인터페이스를 구현한 Fragment를 instanceof로 체크한 다음 onUpdateData를 호출해서 show가 되는 Fragment를 갱신한다.
4. 샘플코드
소스코드
APK
0. 탭 화면을 구성할 때 주의사항.
네비게이션 흐름상 ListFragment에서 ListItemFragment로 이동해야 하는 경우
getSupportFragmentManager().beginTransaction().replace(R.id.tab_container, ListItemFragment.newInstance()).commit();
형태로 사용하는 경우가 종종 있지만
Tab 내에서 UI 이동이 발생하면 Fragment 흐름을 관리하기 어렵다.
Tab을 사용할 때는 Activity로 List 항목을 표현하는 것을 추천.
1. 탭 구성
Tab1Fragment, Tab2Fragment, Tab3Fragment를 생성한 뒤
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.findFragmentById(R.id.tab_container) == null) {
Tab1Fragment tab1 = Tab1Fragment.newInstance("Home Tab");
Tab2Fragment tab2 = Tab2Fragment.newInstance("Dash Board Tab");
Tab3Fragment tab3 = Tab3Fragment.newInstance("Notification Tab");
fragmentManager.beginTransaction()
.add(R.id.tab_container, tab1, tab1.getClass().getSimpleName()).show(tab1)
.add(R.id.tab_container, tab2, tab2.getClass().getSimpleName()).hide(tab2)
.add(R.id.tab_container, tab3, tab3.getClass().getSimpleName()).hide(tab3).commit();
}
첫번째 탭만 show를 하고 나머지 탭은 모두 hide 처리
Activity의 onCreate에서 위 코드를 적용하면 FragmentManager에 적용된 순서와 동일하게
onCreate > onCreateView > onResume
상태가 된다.
2. Tab 이동
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
List<Fragment> fragments = fragmentManager.getFragments();
for (Fragment item : fragments) {
if (item.getClass().getSimpleName().equals(tabName)) {
beginTransaction.show(item);
} else {
beginTransaction.hide(item);
}
}
beginTransaction.commit();
getFragments 메소드를 통해서 현재 탭을 모두 가져와서 보여질 Fragment만 show 처리하고 나머지 Fragment는 모두 hide 처리한다.
3. 탭 데이터 갱신
동일한 탭 또는 다른탭을 선택 했을 때 Fragment 내용을 변경해야 한다면 인터페이스를 적용해서 데이터를 갱신한다.
- 인터페이스
public interface OnDataUpdate {
/**
* 데이터 갱신
*/
void onUpdateData();
}
- 인터페이스 구현
public class Tab1Fragment extends Fragment implements OnDataUpdate {
@Override
public void onUpdateData() {
Log.d("Fragment", text + " : onUpdateData");
count++;
TextView textView = getView().findViewById(R.id.text);
textView.setText(String.format("%s %d", text, count));
}
}
- 데이터 갱신
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
List<Fragment> fragments = fragmentManager.getFragments();
for (Fragment item : fragments) {
if (item.getClass().getSimpleName().equals(tabName)) {
beginTransaction.show(item);
/*보여질 탭 갱신*/
if (item instanceof OnDataUpdate)
((OnDataUpdate) item).onUpdateData();
} else {
beginTransaction.hide(item);
}
}
beginTransaction.commit();
OnDataUpdate 인터페이스를 구현한 Fragment를 instanceof로 체크한 다음 onUpdateData를 호출해서 show가 되는 Fragment를 갱신한다.
4. 샘플코드
소스코드
APK
2019년 3월 21일 목요일
안드로이드 QR 코드 리더 예제
- 오픈소스 라이브러리를 이용한 예제
- ZXing 공식 페이지
https://github.com/zxing/zxing
1. 기본코드
- 어플리케이션 build.gradle
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
- AndroidManifest.xml
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="android:screenOrientation" />
> tools:replace="android:screenOrientation"을 추가하지 않으면 android:screenOrientation="fullSensor" 에러가 발생한다.
- Activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.action).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new IntentIntegrator(MainActivity.this).initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() != null) {
String text = result.getContents();
if (TextUtils.isEmpty(text) == false) {
TextView textView = findViewById(R.id.text);
textView.setText(text);
}
Log.d(TAG, "QR : " + result.getContents());
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
> initiateScan(); 메소드를 호출하면 CaptureActivity가 호출되고 카메라 사용 퍼미션에 대한 안내가 표시된다.
2. 샘플코드
소스코드
APK
- QR
- ZXing 공식 페이지
https://github.com/zxing/zxing
1. 기본코드
- 어플리케이션 build.gradle
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
- AndroidManifest.xml
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="android:screenOrientation" />
> tools:replace="android:screenOrientation"을 추가하지 않으면 android:screenOrientation="fullSensor" 에러가 발생한다.
- Activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.action).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new IntentIntegrator(MainActivity.this).initiateScan();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() != null) {
String text = result.getContents();
if (TextUtils.isEmpty(text) == false) {
TextView textView = findViewById(R.id.text);
textView.setText(text);
}
Log.d(TAG, "QR : " + result.getContents());
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
> initiateScan(); 메소드를 호출하면 CaptureActivity가 호출되고 카메라 사용 퍼미션에 대한 안내가 표시된다.
2. 샘플코드
소스코드
APK
- QR
2019년 3월 18일 월요일
ShareActionProvider 예제 - androidx
- 기본코드
MenuItem ic_menu_share = menu.add("share").setIcon(android.R.drawable.ic_menu_share);
shareActionProvider = new ShareActionProvider(this);
ic_menu_share.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setActionProvider(ic_menu_share, shareActionProvider);
- createChooser 호출
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("http://www.google.com"));
shareActionProvider.setShareIntent(intent);
startActivity(Intent.createChooser(intent, "share"));
> createChooser를 호출하면 하단에 선택 메뉴가 표시.
> setShareIntent는 액션바 메뉴에 표시.
MenuItem ic_menu_share = menu.add("share").setIcon(android.R.drawable.ic_menu_share);
shareActionProvider = new ShareActionProvider(this);
ic_menu_share.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setActionProvider(ic_menu_share, shareActionProvider);
- createChooser 호출
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("http://www.google.com"));
shareActionProvider.setShareIntent(intent);
startActivity(Intent.createChooser(intent, "share"));
> createChooser를 호출하면 하단에 선택 메뉴가 표시.
> setShareIntent는 액션바 메뉴에 표시.
라벨:
안드로이드,
androidx,
ShareActionProvider
위치:
대한민국
안드로이드 화면녹화 예제 - MediaProjection
- 안드로이드 API 21 이상
- WRITE_EXTERNAL_STORAGE, RECORD_AUDIO 권한 필요.
1. 기본코드
- MediaRecorder
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFile);
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mediaRecorder.setVideoSize(displayMetrics.widthPixels, displayMetrics.heightPixels);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setVideoEncodingBitRate(512 * 1000);
mediaRecorder.setVideoFrameRate(30);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
- MediaProjection
final MediaRecorder screenRecorder = createRecorder();
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
MediaProjection.Callback callback = new MediaProjection.Callback() {
@Override
public void onStop() {
super.onStop();
if (screenRecorder != null) {
screenRecorder.stop();
screenRecorder.reset();
screenRecorder.release();
}
mediaProjection.unregisterCallback(this);
mediaProjection = null;
}
};
mediaProjection.registerCallback(callback, null);
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mediaProjection.createVirtualDisplay(
"sample",
displayMetrics.widthPixels, displayMetrics.heightPixels, displayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
screenRecorder.getSurface(), null, null);
screenRecorder.start();
> createVirtualDisplay 과정에서 MediaRecorder의 Surface를 가져와 버추얼 디스플레이로 녹화를 한다.
> 녹화를 위해서 오디오와 저장소 사용 권한을 허용해야 한다.
- 전체코드
public class MainActivity extends AppCompatActivity {
private static final String videoFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/MediaProjection.mp4";
private static final int REQUEST_CODE_PERMISSIONS = 100;
private static final int REQUEST_CODE_MediaProjection = 101;
private MediaProjection mediaProjection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 퍼미션 확인
checkSelfPermission();
}
@Override
protected void onDestroy() {
// 녹화중이면 종료하기
if (mediaProjection != null) {
mediaProjection.stop();
}
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, final int resultCode, @Nullable final Intent data) {
// 미디어 프로젝션 응답
if (requestCode == REQUEST_CODE_MediaProjection && resultCode == RESULT_OK) {
screenRecorder(resultCode, data);
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 음성녹음, 저장소 퍼미션
*
* @return
*/
public boolean checkSelfPermission() {
String temp = "";
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
temp += Manifest.permission.RECORD_AUDIO + " ";
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
temp += Manifest.permission.WRITE_EXTERNAL_STORAGE + " ";
}
if (TextUtils.isEmpty(temp) == false) {
ActivityCompat.requestPermissions(this, temp.trim().split(" "), REQUEST_CODE_PERMISSIONS);
return false;
} else {
initView();
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_PERMISSIONS: {
int length = permissions.length;
for (int i = 0; i < length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
// 퍼미션 동의가 1개라도 부족하면 화면을 초기화 하지 않음
return;
}
initView();
}
return;
}
default:
return;
}
}
/**
* 뷰 초기화
*/
private void initView() {
findViewById(R.id.actionRec).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 미디어 프로젝션 요청
startMediaProjection();
}
});
}
/**
* 화면녹화
*
* @param resultCode
* @param data
*/
private void screenRecorder(int resultCode, @Nullable Intent data) {
final MediaRecorder screenRecorder = createRecorder();
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
MediaProjection.Callback callback = new MediaProjection.Callback() {
@Override
public void onStop() {
super.onStop();
if (screenRecorder != null) {
screenRecorder.stop();
screenRecorder.reset();
screenRecorder.release();
}
mediaProjection.unregisterCallback(this);
mediaProjection = null;
}
};
mediaProjection.registerCallback(callback, null);
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mediaProjection.createVirtualDisplay(
"sample",
displayMetrics.widthPixels, displayMetrics.heightPixels, displayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
screenRecorder.getSurface(), null, null);
final Button actionRec = findViewById(R.id.actionRec);
actionRec.setText("STOP REC");
actionRec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
actionRec.setText("START REC");
if (mediaProjection != null) {
mediaProjection.stop();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(videoFile), "video/mp4");
startActivity(intent);
}
}
});
screenRecorder.start();
}
/**
* 미디어 프로젝션 요청
*/
private void startMediaProjection() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE_MediaProjection);
}
}
/**
* 미디어 레코더
*
* @return
*/
private MediaRecorder createRecorder() {
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFile);
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mediaRecorder.setVideoSize(displayMetrics.widthPixels, displayMetrics.heightPixels);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setVideoEncodingBitRate(512 * 1000);
mediaRecorder.setVideoFrameRate(30);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
return mediaRecorder;
}
}
2. 샘플코드
소스코드
APK
- WRITE_EXTERNAL_STORAGE, RECORD_AUDIO 권한 필요.
1. 기본코드
- MediaRecorder
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFile);
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mediaRecorder.setVideoSize(displayMetrics.widthPixels, displayMetrics.heightPixels);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setVideoEncodingBitRate(512 * 1000);
mediaRecorder.setVideoFrameRate(30);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
- MediaProjection
final MediaRecorder screenRecorder = createRecorder();
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
MediaProjection.Callback callback = new MediaProjection.Callback() {
@Override
public void onStop() {
super.onStop();
if (screenRecorder != null) {
screenRecorder.stop();
screenRecorder.reset();
screenRecorder.release();
}
mediaProjection.unregisterCallback(this);
mediaProjection = null;
}
};
mediaProjection.registerCallback(callback, null);
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mediaProjection.createVirtualDisplay(
"sample",
displayMetrics.widthPixels, displayMetrics.heightPixels, displayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
screenRecorder.getSurface(), null, null);
screenRecorder.start();
> createVirtualDisplay 과정에서 MediaRecorder의 Surface를 가져와 버추얼 디스플레이로 녹화를 한다.
> 녹화를 위해서 오디오와 저장소 사용 권한을 허용해야 한다.
- 전체코드
public class MainActivity extends AppCompatActivity {
private static final String videoFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/MediaProjection.mp4";
private static final int REQUEST_CODE_PERMISSIONS = 100;
private static final int REQUEST_CODE_MediaProjection = 101;
private MediaProjection mediaProjection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 퍼미션 확인
checkSelfPermission();
}
@Override
protected void onDestroy() {
// 녹화중이면 종료하기
if (mediaProjection != null) {
mediaProjection.stop();
}
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, final int resultCode, @Nullable final Intent data) {
// 미디어 프로젝션 응답
if (requestCode == REQUEST_CODE_MediaProjection && resultCode == RESULT_OK) {
screenRecorder(resultCode, data);
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 음성녹음, 저장소 퍼미션
*
* @return
*/
public boolean checkSelfPermission() {
String temp = "";
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
temp += Manifest.permission.RECORD_AUDIO + " ";
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
temp += Manifest.permission.WRITE_EXTERNAL_STORAGE + " ";
}
if (TextUtils.isEmpty(temp) == false) {
ActivityCompat.requestPermissions(this, temp.trim().split(" "), REQUEST_CODE_PERMISSIONS);
return false;
} else {
initView();
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_PERMISSIONS: {
int length = permissions.length;
for (int i = 0; i < length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
// 퍼미션 동의가 1개라도 부족하면 화면을 초기화 하지 않음
return;
}
initView();
}
return;
}
default:
return;
}
}
/**
* 뷰 초기화
*/
private void initView() {
findViewById(R.id.actionRec).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 미디어 프로젝션 요청
startMediaProjection();
}
});
}
/**
* 화면녹화
*
* @param resultCode
* @param data
*/
private void screenRecorder(int resultCode, @Nullable Intent data) {
final MediaRecorder screenRecorder = createRecorder();
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
MediaProjection.Callback callback = new MediaProjection.Callback() {
@Override
public void onStop() {
super.onStop();
if (screenRecorder != null) {
screenRecorder.stop();
screenRecorder.reset();
screenRecorder.release();
}
mediaProjection.unregisterCallback(this);
mediaProjection = null;
}
};
mediaProjection.registerCallback(callback, null);
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mediaProjection.createVirtualDisplay(
"sample",
displayMetrics.widthPixels, displayMetrics.heightPixels, displayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
screenRecorder.getSurface(), null, null);
final Button actionRec = findViewById(R.id.actionRec);
actionRec.setText("STOP REC");
actionRec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
actionRec.setText("START REC");
if (mediaProjection != null) {
mediaProjection.stop();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(videoFile), "video/mp4");
startActivity(intent);
}
}
});
screenRecorder.start();
}
/**
* 미디어 프로젝션 요청
*/
private void startMediaProjection() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE_MediaProjection);
}
}
/**
* 미디어 레코더
*
* @return
*/
private MediaRecorder createRecorder() {
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFile);
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
mediaRecorder.setVideoSize(displayMetrics.widthPixels, displayMetrics.heightPixels);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setVideoEncodingBitRate(512 * 1000);
mediaRecorder.setVideoFrameRate(30);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
return mediaRecorder;
}
}
2. 샘플코드
소스코드
APK
라벨:
안드로이드,
화면녹화,
MediaProjection
위치:
대한민국
2019년 3월 14일 목요일
ExoPlayer 예제
- 구글 ExoPlayer
https://github.com/google/ExoPlayer
https://developer.android.com/guide/topics/media/exoplayer
- 기본 미디어 플레이와 HLS 미디어 플레이 예제
1. 예제코드
- 어플리케이션 build.gradle 설정
implementation 'com.google.android.exoplayer:exoplayer:2.8.4'
- 기본코드
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static class EventListener extends Player.DefaultEventListener {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
String stateString;
switch (playbackState) {
case Player.STATE_IDLE:
stateString = "ExoPlayer.STATE_IDLE";
break;
case Player.STATE_BUFFERING:
stateString = "ExoPlayer.STATE_BUFFERING";
break;
case Player.STATE_READY:
stateString = "ExoPlayer.STATE_READY";
break;
case Player.STATE_ENDED:
stateString = "ExoPlayer.STATE_ENDED";
break;
default:
stateString = "UNKNOWN_STATE";
break;
}
Log.d(TAG, "changed state to " + stateString + ", playWhenReady: " + playWhenReady);
}
}
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private SimpleExoPlayer exoPlayer;
private long playbackPosition;
private int currentWindow;
private boolean playWhenReady = true;
private EventListener eventListener = new EventListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
initPlayer();
}
@Override
protected void onPause() {
super.onPause();
releasePlayer();
}
@Override
protected void onStop() {
super.onStop();
releasePlayer();
}
private void initPlayer() {
String url = "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8";
PlayerView playerView = findViewById(R.id.playerView);
TrackSelection.Factory adaptiveTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(adaptiveTrackSelectionFactory), new DefaultLoadControl());
exoPlayer.addListener(new EventListener());
MediaSource mediaSource = url.endsWith(".m3u8") ? buildMediaSourceHLS(Uri.parse(url)) : buildMediaSourceVideo(Uri.parse(url));
exoPlayer.prepare(mediaSource, true, false);
exoPlayer.setPlayWhenReady(playWhenReady);
exoPlayer.seekTo(currentWindow, playbackPosition);
playerView.setPlayer(exoPlayer);
}
private void releasePlayer() {
if (exoPlayer != null) {
playbackPosition = exoPlayer.getCurrentPosition();
currentWindow = exoPlayer.getCurrentWindowIndex();
playWhenReady = exoPlayer.getPlayWhenReady();
exoPlayer.removeListener(eventListener);
exoPlayer.release();
exoPlayer = null;
}
}
private MediaSource buildMediaSourceHLS(Uri uri) {
String userAgent = System.getProperty("http.agent");
Log.d(TAG, "UserAgent : " + userAgent);
DataSource.Factory manifestDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
HlsMediaSource hlsMediaSource = new HlsMediaSource.Factory(manifestDataSourceFactory).createMediaSource(uri);
return new ConcatenatingMediaSource(hlsMediaSource);
}
private MediaSource buildMediaSourceVideo(Uri uri) {
String userAgent = System.getProperty("http.agent");
Log.d(TAG, "UserAgent : " + userAgent);
ExtractorMediaSource videoSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory(TextUtils.isEmpty(userAgent) ? "DefaultHttpDataSourceFactory" : userAgent)).
createMediaSource(uri);
return new ConcatenatingMediaSource(videoSource);
}
}
> buildMediaSourceHLS 메소드는 HLS 미디어를 재생하기위한 메소드
> 예제에서는 PlayerView를 통해 ExoPlayer를 재생하는 예제.
> 안드로이드 MediaPlayer과 동일하게 구현 가능.
> 주요 특징은 별도 검색으로 확인해 볼 것.
> 사용된 HSL 미디어 소스
https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8
추가) 오디오와 영상이 분리된 경우
private MediaSource buildMediaSourceAdaptive(Uri video, Uri audio) {
ExtractorMediaSource videoSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(video);
ExtractorMediaSource audioSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(audio);
MergingMediaSource mergingMediaSource = new MergingMediaSource(videoSource, audioSource);
return new ConcatenatingMediaSource(mergingMediaSource);
}
2. 샘플코드
소스코드
APK
https://github.com/google/ExoPlayer
https://developer.android.com/guide/topics/media/exoplayer
- 기본 미디어 플레이와 HLS 미디어 플레이 예제
1. 예제코드
- 어플리케이션 build.gradle 설정
implementation 'com.google.android.exoplayer:exoplayer:2.8.4'
- 기본코드
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static class EventListener extends Player.DefaultEventListener {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
String stateString;
switch (playbackState) {
case Player.STATE_IDLE:
stateString = "ExoPlayer.STATE_IDLE";
break;
case Player.STATE_BUFFERING:
stateString = "ExoPlayer.STATE_BUFFERING";
break;
case Player.STATE_READY:
stateString = "ExoPlayer.STATE_READY";
break;
case Player.STATE_ENDED:
stateString = "ExoPlayer.STATE_ENDED";
break;
default:
stateString = "UNKNOWN_STATE";
break;
}
Log.d(TAG, "changed state to " + stateString + ", playWhenReady: " + playWhenReady);
}
}
private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private SimpleExoPlayer exoPlayer;
private long playbackPosition;
private int currentWindow;
private boolean playWhenReady = true;
private EventListener eventListener = new EventListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
initPlayer();
}
@Override
protected void onPause() {
super.onPause();
releasePlayer();
}
@Override
protected void onStop() {
super.onStop();
releasePlayer();
}
private void initPlayer() {
String url = "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8";
PlayerView playerView = findViewById(R.id.playerView);
TrackSelection.Factory adaptiveTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(adaptiveTrackSelectionFactory), new DefaultLoadControl());
exoPlayer.addListener(new EventListener());
MediaSource mediaSource = url.endsWith(".m3u8") ? buildMediaSourceHLS(Uri.parse(url)) : buildMediaSourceVideo(Uri.parse(url));
exoPlayer.prepare(mediaSource, true, false);
exoPlayer.setPlayWhenReady(playWhenReady);
exoPlayer.seekTo(currentWindow, playbackPosition);
playerView.setPlayer(exoPlayer);
}
private void releasePlayer() {
if (exoPlayer != null) {
playbackPosition = exoPlayer.getCurrentPosition();
currentWindow = exoPlayer.getCurrentWindowIndex();
playWhenReady = exoPlayer.getPlayWhenReady();
exoPlayer.removeListener(eventListener);
exoPlayer.release();
exoPlayer = null;
}
}
private MediaSource buildMediaSourceHLS(Uri uri) {
String userAgent = System.getProperty("http.agent");
Log.d(TAG, "UserAgent : " + userAgent);
DataSource.Factory manifestDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
HlsMediaSource hlsMediaSource = new HlsMediaSource.Factory(manifestDataSourceFactory).createMediaSource(uri);
return new ConcatenatingMediaSource(hlsMediaSource);
}
private MediaSource buildMediaSourceVideo(Uri uri) {
String userAgent = System.getProperty("http.agent");
Log.d(TAG, "UserAgent : " + userAgent);
ExtractorMediaSource videoSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory(TextUtils.isEmpty(userAgent) ? "DefaultHttpDataSourceFactory" : userAgent)).
createMediaSource(uri);
return new ConcatenatingMediaSource(videoSource);
}
}
> buildMediaSourceHLS 메소드는 HLS 미디어를 재생하기위한 메소드
> 예제에서는 PlayerView를 통해 ExoPlayer를 재생하는 예제.
> 안드로이드 MediaPlayer과 동일하게 구현 가능.
> 주요 특징은 별도 검색으로 확인해 볼 것.
> 사용된 HSL 미디어 소스
https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8
추가) 오디오와 영상이 분리된 경우
private MediaSource buildMediaSourceAdaptive(Uri video, Uri audio) {
ExtractorMediaSource videoSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(video);
ExtractorMediaSource audioSource =
new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(audio);
MergingMediaSource mergingMediaSource = new MergingMediaSource(videoSource, audioSource);
return new ConcatenatingMediaSource(mergingMediaSource);
}
2. 샘플코드
소스코드
APK
피드 구독하기:
글 (Atom)