2013-08-30 51 views
0

我正在構建一個android應用程序。如果它存在於我的/ res/raw文件夾中,我只想播放一個wav文件。下面的代碼不起作用,因爲f.exists()總是計算爲false:Android無法在我的res/raw文件夾中播放wav文件

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(); 
     } 

我還應該做些什麼才能使其工作?

回答

1

嘗試通過其它方式來加載WAV:

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

註釋1

你不需要在我的情況下使用_ring.prepare();MediaPlayer.create需要關心

評論2

此外,由於加載從原材料WAV資源,你不需要if(f.exists())衆所周知聲明

+0

完美塔tworked – John