【问题标题】:Android can't play a wav file in my res/raw folderAndroid 无法播放我的 res/raw 文件夹中的 wav 文件
【发布时间】:2013-08-30 21:20:40
【问题描述】:

我正在构建一个安卓应用。如果它存在于我的 /res/raw 文件夹中,我只想播放一个 wav 文件。以下代码不起作用,因为 f.exists() 总是评估为 false:

    String filePathString = "android.resource://"+_context.getPackageName()+"/res/raw/oldphone_mono.wav";
    File f = new File(filePathString);

    java.io.InputStream inputStream = _context.getResources().openRawResource(com.hello20.R.raw.oldphone_mono);


    if(f.exists()) {

        Uri ringUri = Uri.parse(filePathString);

        _ring = new MediaPlayer();
        _ring.setDataSource(_context, ringUri);

        final AudioManager audioManager = (AudioManager) _context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
            _ring.setAudioStreamType(AudioManager.STREAM_RING);
            _ring.setLooping(true);
            _ring.prepare();
            _ring.start();
        }

我还应该做些什么来完成这项工作?

【问题讨论】:

    标签: android


    【解决方案1】:

    尝试通过其他方式加载wav:

     _ring=MediaPlayer.create(_context,R.raw.oldphone_mono);
     _ring.start();
     _ring.setLooping( true );
    

    评论 1

    在我的情况下,您不需要使用_ring.prepare();MediaPlayer.create关心

    评论 2

    此外,由于您从 raw 加载 wav 资源,因此您不需要 if(f.exists()) statemant

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 2023-03-03
      • 2014-06-17
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多