activity_main.xml
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://chemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/tan_background" android:orientation="vertical" tools:context="com.example.android.miwok.MainActivity"> <Button android:id="@+id/play_button" android:text="Play" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/pause_button" android:text="Pause" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.example.android.musicplayer; import public class MainActivity extends AppCompactActivity { private MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mediaPlayer = MediaPlayer.create(this, R.raw.android_trailer); Button playButton = (Button)findViewById(R.id.play_button); playButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ mediaPlayer.start(); } }); Button pauseButton = (Button)findViewById(R.id.play_button); playButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ mediaPlayer.pause(); } }); } }