0

I have tried to create a simple android Media Player app in Eclipse which play songs from sdcard. All the songs are showing on the screen as a list but do not play when clicked. Here is my MainActivity.java file :

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;




class Mp3Filter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3"));
    }
}

public class MainActivity extends ListActivity {

    Button stop, play;
    String SD_PATH = Environment.getExternalStorageDirectory().getPath();
    List<String> songs = new ArrayList<String>();
    MediaPlayer mp = new MediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        updatePlaylist();

        stop = (Button) findViewById(R.id.button1);
        play = (Button) findViewById(R.id.button2);
        play.setVisibility(View.GONE);


        stop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mp.stop();              
            }
        });
        play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
             mp.pause();
             if(mp.isPlaying()==false){
                 play.setText("Resume");
             }
            }
        });
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        try{
            mp.reset();
            mp.setDataSource(SD_PATH + songs.get(position));
            mp.prepare();
            mp.start();
            if(mp.isPlaying()== true){
                play.setText("Pause");
                play.setVisibility(View.VISIBLE);
            }

        } catch(IOException e){
            Log.v(getString(R.string.app_name),e.getMessage());
        }
    }


    private void updatePlaylist() {
        // TODO Auto-generated method stub
        File home = new File(SD_PATH);
        if(home.listFiles( new Mp3Filter()).length > 0){
            for(File file : home.listFiles( new Mp3Filter())) {
                songs.add(file.getName());
            }
            ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_items,songs);
            setListAdapter(songList);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Activity_Main.xml file :

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.studs.mediaplayer.MainActivity" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="50dp" >
    </ListView>

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/android:list"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="23dp"
        android:text="@string/pp" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/android:list"
        android:layout_marginRight="52dp"
        android:text="@string/st" />

</RelativeLayout>

and song_items.xml file :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


</TextView>

Thanks in advance!

2
  • 1
    do you have any warning/error in your logcat? also make sure you set the correct mp.setDataSource()
    – ColdFire
    Aug 6, 2014 at 15:03
  • Nothing is there in logcat. Aug 6, 2014 at 15:06

1 Answer 1

0

Your problem is in this line

mp.setDataSource(SD_PATH + songs.get(position));

songs.get(position) is only the file's name and since the file could be in some sub folder of your sdcard then you're not setting the correct data source..

Update

may be you can use a second list

List<String> songsPaths = new ArrayList<String>();

then update your method

private void updatePlaylist() {
    // TODO Auto-generated method stub
    File home = new File(SD_PATH);
    if(home.listFiles( new Mp3Filter()).length > 0){
        for(File file : home.listFiles( new Mp3Filter())) {
            songs.add(file.getName());
            songsPaths.add(file.getAbsolutePath());
        }
        ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_items,songs);
        setListAdapter(songList);
    }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    try{
        mp.reset();
        mp.setDataSource(songsPaths.get(position));
        mp.prepare();
        mp.start();
        if(mp.isPlaying()== true){
            play.setText("Pause");
            play.setVisibility(View.VISIBLE);
        }

    } catch(IOException e){
        Log.v(getString(R.string.app_name),e.getMessage());
    }
}
2
  • thanks Boss!!. Now songs are playing but there is a warning [2014-08-06 08:24:42 - MediaPlayer] ActivityManager: Warning: Activity not started, its current task has been brought to the front </TextView> Aug 6, 2014 at 15:27
  • clean your project and it'll be fine.. don't forget the mark the answer
    – ColdFire
    Aug 6, 2014 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.