Create an application that will play a media file from the memory card.
MainActivity.java
MainActivity.java
package com.example.assignment17;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Media;
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.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
Button btnpre,btnstart,btnnext;
String song[];
MediaPlayer player;
ListView lv;
boolean flag=false;
static int j=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnpre=(Button) findViewById(R.id.btnpre);
btnstart=(Button) findViewById(R.id.btnstart);
btnnext=(Button) findViewById(R.id.btnnext);
lv=(ListView) findViewById(R.id.list);
btnpre.setOnClickListener(this);
btnstart.setOnClickListener(this);
btnnext.setOnClickListener(this);
lv.setOnItemClickListener(this);
btnpre.setTextColor(Color.GREEN);
btnstart.setTextColor(Color.GREEN);
btnnext.setTextColor(Color.GREEN);
try {
Cursor cur = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, null, null, null);
song = new String[cur.getCount()];
int i = 0;
Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(cur));
while (cur.moveToNext()) {
String path = cur.getString(cur.getColumnIndex(Media.DATA));
song[i] = path;
i++;
}
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, song);
lv.setAdapter(aa);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
public void song_play()
{
try {
if (flag == true)
player.stop();
player = new MediaPlayer();
player.setDataSource(song[j]);
player.prepare();
player.start();
flag = true;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Play" + j, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
Button action = (Button) v;
if (v.getId() == btnpre.getId()) {
if (j > 0) {
j--;
song_play();
} else {
song_play();
}
} else if (action.getText().toString().equals("|>")) {
song_play();
btnstart.setBackgroundResource(R.drawable.pause);
btnstart.setText("||");
} else if (action.getText().toString().equals("||")) {
player.stop();
btnstart.setBackgroundResource(R.drawable.play);
btnstart.setText("|>");
} else if (v.getId() == btnnext.getId()) {
if (song.length - 1 > j) {
j++;
song_play();
} else {
song_play();
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
j = position;
song_play();
btnstart.setText("||");
btnstart.setBackgroundResource(R.drawable.pause);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="393dp"
android:layout_height="473dp"
android:layout_x="8dp"
android:layout_y="243dp" />
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Select Your Song"
android:gravity="center"
android:layout_y="210dp"
android:layout_x="3dp"
android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/btnstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="165dp"
android:layout_y="148dp"
android:background="@drawable/play"
android:text="" />
<Button
android:id="@+id/btnpre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="47dp"
android:layout_y="150dp"
android:background="@drawable/previous"
android:text="" />
<Button
android:id="@+id/btnnext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="258dp"
android:layout_y="150dp"
android:background="@drawable/next"
android:text="" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="166dp"
android:layout_y="74dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView2"
android:layout_width="352dp"
android:layout_height="wrap_content"
android:layout_x="27dp"
android:layout_y="41dp"
android:gravity="center"
android:text="MEDIA PLAYER"
android:textAppearance="?android:attr/textAppearanceLarge" />
</AbsoluteLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment17">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
No comments:
Post a Comment