2012-05-05 33 views
7

我正在創建一個簡單的應用程序,我想播放聲音。在按下應用程序上的按鈕播放時,它會播放聲音。 播放聲音我正在使用:如何從Android設備中取出音頻文件的路徑或uri?

MediaPlayer mediaPlayer; 
     mediaPlayer = MediaPlayer.create(this, R.raw.test_cbr); 
     mediaPlayer.setOnCompletionListener(new OnCompletionListener() 
     { 
      @Override 
      public void onCompletion(MediaPlayer mp) 
      { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), "Completed", 
        Toast.LENGTH_LONG).show(); 

      } 
     }); 
mediaPlayer.start(); 

它工作正常。

但現在我希望用戶應該能夠從設備中選擇音頻文件,以便當他或她按下播放按鈕時,應該播放所選的聲音。

我沒有收到像圖片一樣的任何意圖動作。

Intent photo_picker_intent = new Intent(Intent.ACTION_PICK); 
photo_picker_intent.setType("image/*"); 
startActivityForResult(photo_picker_intent, PHOTOS_FROM_GALLERY); 

在上面的代碼中,用戶將簡單地去圖庫。點擊任何圖像後,他或她將獲得一個圖像URI,可用於在應用程序中顯示圖像。我想要做同樣的事情,除了音頻文件選擇。

+0

你需要挑選圖像和音頻? – Dinesh

回答

16

只要使用,這兩條線,

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(Intent.createChooser(intent, "Gallery"), reqCode); 

或者,

Intent intent = new Intent(); 
     intent.setType("audio/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(Intent.createChooser(intent,"Select Audio "), reqCode); 
+0

非常感謝。 – AB1209

相關問題