【问题标题】:Problems with loading sounds from Sd-Card从 SD 卡加载声音的问题
【发布时间】:2012-11-04 18:50:48
【问题描述】:

我有一个应用程序应该从 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 Premission 会出现此错误:

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

【问题讨论】:

  • 请发布您的 logcat 错误,以便我们准确了解发生了什么。
  • 我发布了它们..添加 android.permission.WRITE_EXTERNAL_STORAGE 给了我另一个错误..
  • 您需要WRITE_EXTERNAL_STORAGE 权限才能在 sdcard 上创建文件夹(或其他任何内容),新错误是什么?
  • 11-04 19:58:01.729: E/SoundPool(8022): error loading /mnt/sdcard/Android/data/com.example.idea/files/sound1.mp3
  • sound1.mp3 真的在这个文件夹中吗?

标签: android audio sd-card


【解决方案1】:

出现此错误:

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

你说:

文件存在且位于 /mnt/sdcard/ 文件夹中。应用程序应在外部存储中搜索 sound1.mp3 并加载它(如果存在)。

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

【讨论】:

  • 我不想那样做..我希望 applicationn 在 sdcard 上的任何地方找到 sound1.mp3..
  • Android 明确建议每个应用程序以有组织的方式存储其数据using these directories。但是,您可以使用 Environment.getExternalStorageDirectory() 而不是 context.getExternalFilesDir(null) 将数据聚集在 sdcard 的基本文件夹中
猜你喜欢
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
相关资源
最近更新 更多