[연습문제]
6
activitiy_main.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="horizontal">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listViewMP3">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btnPlay"
android:text="듣기"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btnPause"
android:text="일시정지"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btnStop"
android:text="중지"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMP3"
android:text="실행중인 음악:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvTime"
android:text="진행시간 : "/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pbMP3"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
더보기
package com.cookandroid.project13_1;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listViewMP3;
Button btnPlay, btnStop, btnPause;
TextView tvMP3, tvTime;
ProgressBar pbMP3;
ArrayList<String> mp3List;
String selectedMP3;
String mp3Path = Environment.getExternalStorageDirectory().getPath()+"/";
MediaPlayer mPlayer;
boolean PAUSED = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("간단 MP3 플레이어");
ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, MODE_PRIVATE);
mp3List = new ArrayList<String>();
File[] listFiles = new File(mp3Path).listFiles();
String fileName, extName;
for (File file : listFiles){
fileName = file.getName();
extName = fileName.substring(fileName.length()-3);
if(extName.equals((String)"mp3")){mp3List.add(fileName);}
}
listViewMP3 = (ListView) findViewById(R.id.listViewMP3);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, mp3List);
listViewMP3.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listViewMP3.setAdapter(adapter);
listViewMP3.setItemChecked(0, true);
listViewMP3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedMP3 = mp3List.get(position);
}
});
selectedMP3 = mp3List.get(0);
Handler mp3Handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
SimpleDateFormat timeFormat = new SimpleDateFormat(" mm:ss ");
pbMP3.setProgress(mPlayer.getCurrentPosition());
tvTime.setText("진행 시간 : " + timeFormat.format(mPlayer.getCurrentPosition()));
}
};
btnPlay = (Button) findViewById(R.id.btnPlay);
btnStop = (Button) findViewById(R.id.btnStop);
btnPause = (Button) findViewById(R.id.btnPause);
tvMP3 = (TextView) findViewById(R.id.tvMP3);
tvTime = (TextView) findViewById(R.id.tvTime);
pbMP3 = (ProgressBar) findViewById(R.id.pbMP3);
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
mPlayer = new MediaPlayer();
mPlayer.setDataSource(mp3Path+selectedMP3);
mPlayer.prepare();
mPlayer.start();
btnPlay.setClickable(false);
btnStop.setClickable(true);
btnPause.setClickable(true);
tvMP3.setText("실행중인 음악 : "+selectedMP3);
new Thread() {
public void run() {
if (mPlayer == null) return;
pbMP3.setMax(mPlayer.getDuration());
while (mPlayer.isPlaying()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mp3Handler.sendEmptyMessage(0);
}
});
SystemClock.sleep(200);
}
}
}.start();
pbMP3.setVisibility(View.VISIBLE);
}catch(IOException e){}
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!PAUSED){
btnPause.setText("이어듣기");
mPlayer.pause();
PAUSED = true;
pbMP3.setVisibility(View.INVISIBLE);
}else{
mPlayer.start();
PAUSED = false;
pbMP3.setVisibility(View.VISIBLE);
btnPause.setText("일시정지");
new Thread() {
public void run() {
if (mPlayer == null) return;
pbMP3.setMax(mPlayer.getDuration());
while (mPlayer.isPlaying()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mp3Handler.sendEmptyMessage(0);
}
});
SystemClock.sleep(200);
}
}
}.start();
}
}
});
btnPause.setClickable(false);
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPlayer.stop();
mPlayer.reset();
btnPlay.setClickable(true);
btnStop.setClickable(false);
btnPause.setClickable(false);
tvMP3.setText("실행중인 음악 : ");
pbMP3.setVisibility(View.INVISIBLE);
}
});
btnStop.setClickable(false);
}
}
7
activitiy_main.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="35.865"
map:cameraTargetLng="128.593"
map:cameraZoom="15"
tools:ignore="MissingClass" />
MainActivity.java
더보기
package com.cookandroid.googlemap13_2;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap gMap;
MapFragment mapFrag;
GroundOverlayOptions videoMark;
ArrayList<GroundOverlayOptions> markList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("구글 지도 활용");
mapFrag = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
markList = new ArrayList<GroundOverlayOptions>();
}
@Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.568256, 126.897240), 15));
gMap.getUiSettings().setZoomControlsEnabled(true);
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
videoMark = new GroundOverlayOptions().image(BitmapDescriptorFactory.fromResource(R.drawable.ic)).position(latLng, 100f, 100f);
markList.add(videoMark);
gMap.addGroundOverlay(videoMark);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,1,0,"위성 지도");
menu.add(0,2,0,"일반 지도");
menu.add(0,3,0,"바로전 마크 지우기");
menu.add(0,4,0,"모든 마크 지우기");
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case 1:
gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
return true;
case 2:
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return true;
case 3:
gMap.clear();
markList.remove(markList.size()-1);
for(int i=0; i<markList.size(); i++){
gMap.addGroundOverlay(markList.get(i));
}
return true;
case 4:
gMap.clear();
return true;
}
return true;
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
'DEVELOPMENT' 카테고리의 다른 글
[Android Studio] Chapter 14 연습문제 (0) | 2021.07.09 |
---|---|
[Android Studio] Chapter 14 직접 풀어보기 (0) | 2021.07.09 |
[Android Studio] Chapter 13 직접 풀어보기 (0) | 2021.07.08 |
[Android Studio] Chapter 12 연습문제 (0) | 2021.07.07 |
[Android Studio] Chapter 12 직접 풀어보기 (0) | 2021.07.07 |