想要从data / data / package_name / files路径播放音频文件

时间:2012-01-24 06:36:04

标签: android media-player

在我的应用程序中,我将音频文件存储在data/data/package_name/files/Folder/文件夹中,现在我想使用MediaPlayer的对象播放这些音频文件我尝试了2种方法,但无法实现。

using player.setDataSource(path) //and by using
 MediaPlayer.create(getApplicationContext(),Uri.fromFile(path)).

有没有什么方法可以播放音频。通过上面的尝试,我得到了这个

01-24 11:35:13.968: ERROR/AndroidRuntime(1064): FATAL EXCEPTION: main
01-24 11:35:13.968: ERROR/AndroidRuntime(1064): java.lang.NullPointerException
runtime exception.

请帮帮我。 我也试过

try {
                Log.d("1","1");
                File mFile = new File("/data/data/com.lister.viewer/files/com.aaa.0001/audio.wav");
                Log.d("2","2");
                FileInputStream fileInputStream = new FileInputStream(mFile);
                Log.d("3","3");
                player.setDataSource(fileInputStream.getFD());         
                Log.d("4","4");
                player.prepare();
                Log.d("5","5");

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                Log.d("6","6");
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                Log.d("7","7");
                e.printStackTrace();
            } 

            catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                Log.d("8","8");
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d("9","9");
                e.printStackTrace();
            }
            Log.d("10","10");
            player.start();
            Log.d("11","11");

此代码和日志是

01-24 12:33:31.468: DEBUG/1(11457): 1
01-24 12:33:31.488: DEBUG/2(11457): 2
01-24 12:33:31.488: DEBUG/3(11457): 3
01-24 12:33:31.518: DEBUG/AndroidRuntime(11457): Shutting down VM
01-24 12:33:31.527: WARN/dalvikvm(11457): threadid=1: thread exiting with uncaught 
exception (group=0x4001d800)
01-24 12:33:31.558: ERROR/AndroidRuntime(11457): FATAL EXCEPTION: main
01-24 12:33:31.558: ERROR/AndroidRuntime(11457): java.lang.NullPointerException

当我写一个player = new MediaPlayer()时,我会告诉你更多的事情;上面的文件mFile = new File(“/ data / data / com.lister.viewer / files / com.aaa.0001 / audio.wav”);然后它不会抛出异常但仍然听不到声音,logcat如下所示。

01-24 12:49:59.467: DEBUG/1(16108): 1
01-24 12:49:59.507: DEBUG/2(16108): 2
01-24 12:49:59.507: DEBUG/3(16108): 3
01-24 12:50:00.358: DEBUG/4(16108): 4
01-24 12:50:00.368: DEBUG/5(16108): 5
01-24 12:50:00.368: DEBUG/10(16108): 10
01-24 12:50:00.379: DEBUG/AudioSink(34): bufferCount (4) is too small and increased to 12
01-24 12:50:00.408: DEBUG/11(16108): 11

为什么它给予bufferCount(4)太小并增加到12.我应该在哪里增加它?还需要更多设置吗?

1 个答案:

答案 0 :(得分:0)

好的,盲目地假设,

private MediaPlayer mPlayer = new MediaPlayer();
File mFile = new File("data/data/package_name/files path");
FileInputStream fileInputStream = new FileInputStream(mFile);
mPlayer.reset();
mPlayer.setDataSource(fileInputStream.getFD());         
mPlayer.prepare();

另外,请确保您的音频文件具有世界可读权限..

使用DDMSadb shell ls -al /data/data/package_name/myfile

进行检查