<string.xml>
<resources>
<string name="app_name">노래목록</string>
<string name="title1">사랑이 잘(with 오혁)</string>
<string name="artist1">아이유(IU)</string>
<string name="audio1">iu</string>
<string name="song_image1">img_iu</string>
<string name="time1">00:00:14</string>
</resources>
<MainActivity.java>
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("노래 목록"); }
public void play(View v) {
int id = v.getId();
LinearLayout layout = (LinearLayout)findViewById(id);
String tag = (String)layout.getTag();
Intent it = new Intent(this, AudioImage.class);
it.putExtra("it_tag", tag);
startActivity(it); }}
<activity_main.xml>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/audio1"
android:tag="@string/audio1"
android:clickable="true"
android:onClick="play"
android:background="@drawable/shape_list" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/title1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/artist1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="@string/time1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<AudioImage.java>
public class AudioImage extends AppCompatActivity {
MediaPlayer mp = new MediaPlayer();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_image);
setTitle("노래 재생");
Intent it = getIntent();
String tag = it.getStringExtra("it_tag");
TextView title = (TextView)findViewById(R.id.title);
ImageView song_image = (ImageView)findViewById(R.id.song_image);
Resources res = getResources();
int stringId;
String myKey;
stringId = res.getIdentifier("title"+tag, "string", getPackageName());
myKey = res.getString(stringId);
title.setText(myKey);
stringId = res.getIdentifier("song_image"+tag, "string", getPackageName());
myKey = res.getString(stringId);
int id_image = res.getIdentifier(myKey, "drawable", getPackageName());
song_image.setImageResource(id_image);
stringId = res.getIdentifier("audio"+tag, "string", getPackageName());
myKey = res.getString(stringId);
int id_audio = res.getIdentifier(myKey, "raw", getPackageName());
mp = MediaPlayer.create(this, id_audio);
mp.setLooping(false);
mp.start(); }
public void goBack(View v) {
mp.stop();
mp.release();
finish(); }}
<audio_image.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="wrap_content"
android:orientation="vertical"
android:clickable="true"
android:onClick="goBack"
tools:context="com.example.jinhyukkim.report5_audio2.AudioImage">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/shape_list" />
<ImageView
android:id="@+id/song_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
-------------------------------------------------------------------------
res-drawable 에 img_bigbang.jpg, img_iu.jpg, img_twice.jpg, shape_list.xml 이 있고
res-raw에 bigbang.mp4, iu.mp4, twice.mp4 가 있습니다.
하고자 하는 것은 처음 메인 액티비티에서 해당 레이아웃을 클릭하면 해당 레이아웃에 저장된 값을 가지고 audio_image.xml에 전달해서 audio액티비티에서 타이틀과 앨범 이미지가 뜨면서 노래가 재생되는 것이며, audio_image.xml에서 다시 아무대나 클릭(화면 전체가 레이아웃으로 지정되어 있으므로 레이아웃 클릭으로 봐도 무방)하면 다시 해당 액티비티가 종료되면서 메인화면이 뜨게 되는 것입니다.
값은 맞게 준거 같은데 Activity_main.xml의 화면에서 레이아웃 클릭하면 audio_image.xml화면이 떠야되는데 계속 강제적으로 종료되었다고 합니다...
얘기가 길지만....답변해 주시면 감사하겠습니다.