在ListView上显示音频文件时出错

时间:2016-01-27 16:12:31

标签: android listview

我在.mp3文件夹的列表视图中显示音频res/raw文件时遇到问题。 logcat中的错误是"null pointer exception"。我已经通过使用外部存储尝试了此代码,它通过提供外部存储路径完美地工作。我只需要在应用程序中使用音频文件,以便它可以在手机上运行后显示文件。 下面是代码。请帮帮我!

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

0 个答案:

没有答案