[직접 풀어보기]
1
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="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvMP3"
android:text="실행중인 음악:"/>
<ProgressBar
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.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.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.TextView;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listViewMP3;
Button btnPlay, btnStop, btnPause;
TextView tvMP3;
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);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnStop = (Button) findViewById(R.id.btnStop);
btnPause = (Button) findViewById(R.id.btnPause);
tvMP3 = (TextView) findViewById(R.id.tvMP3);
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);
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("일시정지");
}
}
});
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);
}
}
2
activitiy_main.xml
더보기
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/switch1"
android:text="음악듣기"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pbMP3"/>
</LinearLayout>
MainActivity.java
더보기
package com.cookandroid.testest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
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.Switch;
import android.widget.TextView;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mPlayer;
mPlayer = MediaPlayer.create(this, R.raw.aaa);
final Switch switch1 = (Switch) findViewById(R.id.switch1);
SeekBar pbMP3 = (SeekBar) findViewById(R.id.pbMP3);
switch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (switch1.isChecked()){
mPlayer.start();
new Thread() {
public void run() {
if (mPlayer == null) return;
pbMP3.setMax(mPlayer.getDuration());
while (mPlayer.isPlaying()) {
pbMP3.setProgress(mPlayer.getCurrentPosition());
SystemClock.sleep(200);
}
pbMP3.setProgress(0);
}
}.start();
}
else{mPlayer.stop();}
}
});
pbMP3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){mPlayer.seekTo(progress);}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
3
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="37.568256"
map:cameraTargetLng="126.897240"
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;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap gMap;
MapFragment mapFrag;
GroundOverlayOptions videoMark;
@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);
}
@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);
gMap.addGroundOverlay(videoMark);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,1,0,"위성 지도");
menu.add(0,2,0,"일반 지도");
SubMenu sMenu = menu.addSubMenu("유명장소 바로가기>>");
sMenu.add(0,3,0,"월드컵경기장");
sMenu.add(0,4,0,"한강공원망원야외수영장");
sMenu.add(0,5,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.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.568256, 126.897240), 15));
return true;
case 4:
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.557121, 126.892446), 15));
return true;
case 5:
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.559165, 126.794330), 15));
return true;
}
return true;
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
'DEVELOPMENT' 카테고리의 다른 글
[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 |
[Android Studio] Chapter 11 연습문제 (0) | 2021.07.07 |