android - 从SD卡加载声音时出现问题

标签 android audio sd-card

我有一个应从sd卡加载声音的应用程序,然后单击按钮播放它。当我单击按钮时,我在LogCat中得到这些消息:无法创建外部文件目录,SoundPool错误加载/sound1.mp3,样本0未就绪
这是我的代码:

import java.io.File;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
SoundPool sp;
int mSoundId;
int mStreamId = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        String path = getFullFilePath(getApplicationContext(), "sound1.mp3");
        mSoundId = sp.load(path, 1);

    }
    private String getFullFilePath(Context context, String filename) {  
        File directory = context.getExternalFilesDir(null);
        File file = new File(directory, filename);
        if (!file.canRead()) {
            // error handling
        }
        return file.getAbsolutePath();  
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public void button1(View view){


        if (mStreamId != 0) {
            sp.stop(mStreamId);
        }
        mStreamId = sp.play(mSoundId, 1, 1, 1, 0, 1f);
    }
}

我的LogCat错误:
11-04 18:56:34.499: D/dalvikvm(6547): GC_EXTERNAL_ALLOC freed 46K, 51% free 2686K/5379K, external 0K/0K, paused 57ms
11-04 18:56:34.539: W/ApplicationContext(6547): Unable to create external files directory
11-04 18:58:02.129: E/SoundPool(6644): error loading /sound1.mp3

添加WRITE_EXTERNAL_STORAGE权限会出现以下错误:
11-04 19:58:01.729: E/SoundPool(8022): error loading /mnt/sdcard/Android/data/com.example.idea/files/sound1.mp3

最佳答案

出现此错误:

11-04 19:58:01.729: E/SoundPool(8022): error loading /mnt/sdcard/Android/data/com.example.idea/files/sound1.mp3

您说:

File exists and it is in /mnt/sdcard/ folder. Application should search external storage for sound1.mp3 and load it if it exists.



但是,/mnt/sdcard/不是/mnt/sdcard/Android/data/com.example.idea/files/,只需将sound1.mp3文件移动到应用程序的默认sdcard文件夹中,SoundPool就可以加载它。

关于android - 从SD卡加载声音时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13221708/

相关文章:

python - 为任意音频文件生成缩略图

c - 不断向文件(SDCard)写入数据时,应该多久打开和关闭一次文件?

android - 访问 sdcard 镜像

android - 考虑到内存限制,android 如何处理播放视频?

android - 如何在 editText 中启用覆盖模式

java - React Native : java. util.zip.ZipException : duplicate entry: com/google/android/gms/iid/zzc. 类

android - 从同一应用程序内调用时内容提供者的权限被拒绝

android - 我的 android 模拟器与 honeycomb 颠倒了,我该如何纠正这个问题?

audio - 使用 libavcodec 将 .m4a 转换为 PCM

html - 获取麦克风输入并读取数据