2012-07-24 39 views
1

我產生一個MIDI文件,並把它寫這樣的:的Android的MediaPlayer - 切換到下一首歌曲

File output = new File("exampleout.mid"); 

我想我可能需要改變這一點,所以它是在正確的文件夾(只是一個可讀/寫文件夾,也許音樂/)

接下來我想用MediaPlayer播放這個MIDI文件,但我無法弄清楚如何加載文件。

mediaPlayer = MediaPlayer.create(this, R.raw.test3); 

只從只讀目錄/ res/raw加載。但如果我嘗試這樣的:

mediaPlayer = MediaPlayer.create(this, "file://exampleout"); 

它不起作用,因爲創建需要一個整數作爲輸入。我試用了AssetFileDescriptor,但還沒有弄明白。

同時,我想生成一個新的midi文件並將其加載到mediaPlayer(鏈接)中,以便在第一個文件播放完畢時播放。

MediaPlayer mediaPlayer = MediaPlayer.create(Activity.this,R.raw.a1); 
mediaPlayer.setOnCompletionListener(new musicCompletionListener()); 
mediaPlayer.start(); 
private class musicCompletionListener implements OnCompletionListener { 
    @Override 
    public void onCompletion(MediaPlayer mediaPlayer) { 
mediaPlayer.setDataSource(WHATTOPUTHERE) 
     mediaPlayer.release(); 
    } 
} 

我的問題真的是如何調用該文件。我不能使用字符串,r.raw目錄是不可能的,因爲它的只讀性質。我覺得這個解決方案並不難,但我只是從C++中簡化了java,任何幫助都非常感謝!

+0

只是一句話。我嘗試setDataSource(字符串),但這給了我和IOerror,如http://stackoverflow.com/questions/9625680/mediaplayer-setdatasource-better-to-use-path-or-filedescriptor中所述所以我想我需要一個方式就像,AssetManager assetManager = Context.getAssets(); AssetFileDescriptor fileDescriptor = assetManager.openFd(「a2.mp3」); mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor()); – dorien 2012-07-24 13:26:43

+1

您必須在setDataSource中傳遞Uri,您可以按照以下方式獲取它。 String path =「你存儲動態生成的midi文件的路徑」; Uri midiUri = Uri.parse(path); 希望這會解決你的問題。 – 2012-07-25 06:22:53

+0

@Android_Crazy謝謝。我嘗試過這個。它給了我'Uri無法在'setDataSource(Uri)'上解析成變量''。我這樣做:'String path =「/data/data/com.example.optimuse/exampleout.mid」; \t \t Uri midiUri = Uri.parse(path); \t \t \t \t // String filePath = topDirFile.getAbsolutePath()+ File.separator +「exampleout.mid」; \t嘗試{ \t \t \t mediaPlayer.setDataSource(Uri); \t \t //mediaPlayer.setDataSource("/res/raw/test3「); \t \t} catch(Exception e1){ \t \t \t e1.printStackTrace(); \t} – dorien 2012-07-25 09:55:34

回答

1

我將該文件存儲在緩存目錄中,該工作!

File output = new File(getCacheDir() + "/exampleout.mid"); 

,然後調用該文件:

String filePath = null; 
    File file = null; 
    FileInputStream inputStream = null; 
try { 
      filePath = getCacheDir() + "/exampleout.mid"; 
      file = new File(filePath); 

      inputStream = new FileInputStream(file); 
      if(inputStream.getFD().valid()) 
      { 
       System.out.println("Valid!"); 
      } 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
      System.exit(-1); 
     } 

     try { 
     mediaPlayer = new MediaPlayer(); 
     mediaPlayer.setDataSource(inputStream.getFD()); 
     inputStream.close(); 
    } catch (Exception e1) { 
     e1.printStackTrace(); 
     System.exit(-1); 
    } 

    try { 
     mediaPlayer.prepare(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
+0

很高興看到解決方案。 – 2012-07-25 12:45:27

0

你必須很好地設計你的應用程序的體系結構,然後只有你可以實現你所要求的。

根據我的說法,你可以做到以下幾點。

  1. 設計,其中包含媒體播放器
  2. 方法midiReady(MIDI文件的URI)
  3. 此接口由您的活動實現的接口,只要MIDI文件的生成完成調用該方法midiReady(URI的MIDI文件)
  4. 現在,因爲你的活動實現了界面回調在你的活動中被調用,你可以設置mediaplayer播放midi文件,因爲在回調中你有你的MIDI文件的URI。

上述metinoed點只是一個微弱的想法,你可以做。有了上述微弱的想法,你可以前進實施。

希望這會有所幫助。 謝謝。

+0

謝謝,但是midi文件在需要播放之前就已經準備好了。這就是爲什麼我想使用onCompletion()。我只是不知道播放第二個文件的方式,我無法找到MediaPlayer的良好文檔。 – dorien 2012-07-24 12:23:29

+0

您可以在完成調用後立即實施一個midi文件隊列,您可以從隊列中取出midi文件並播放它。 – 2012-07-24 12:55:11

+0

是的,我認爲那會奏效。我實際上停留在更基本的層面,即如何調用文件(請參閱問題)。任何幫助深表感謝! – dorien 2012-07-24 13:15:08

相關問題