0

I'm having problem while displaying audio .mp3 files in a list view from res/raw folder. The error in the logcat is "null pointer exception". I've tried this code by using External storage and it works perfectly fine by giving the external storage path. All I need is to use audio files in the application so that it can display the files after running on phone. Below is the code. Please help me out!

MainActivity.java

package com.example.testproject;

import java.io.File;

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

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;


class mp3filter implements FilenameFilter{


    public boolean accept(File dir, String filename) {

        return filename.endsWith(".mp3");
    }

}
public class MainActivity extends ListActivity {

    Cursor cursor;
    ListView list;
    private static final String FILE_NAME = "Make Folder";
    Uri path = Uri.parse("android.resource://com.example.testproject/res/raw/anham_with_trans");
    private ArrayList<String> voice = new ArrayList<String>();
    private MediaPlayer mp = new MediaPlayer();


    Button btn;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) this.findViewById(android.R.id.list);

        updatePlayList();

        btn = (Button) this.findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                mp.stop();

            }
        });
    }
    protected void onListItemClick(ListView list, View view, int position, long id)
    {
        try{
            mp.reset();
            mp.setDataSource(path + voice.get(position));
            mp.prepare();
            mp.start();

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

    private void updatePlayList() {

        String uri_path;
        uri_path = path.toString();

        File home = new File(uri_path);
        if(home.listFiles(new mp3filter()).length>0)
        {
            for(File file: home.listFiles(new mp3filter()))
                {
                voice.add(file.getName());

                }
            }
                ArrayAdapter<String> voiceList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, voice);
                setListAdapter(voiceList);



    }







    @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);
    }
}

enter image description here

Mikka
  • 55
  • 1
  • 7

0 Answers0