2011-01-13 183 views
1

如何從我的Android應用程序啓動進程?我想要啓動默認音樂播放器來播放選定的歌曲。我可以從我自己的應用程序中播放歌曲,但我想讓默認播放器播放歌曲。我將如何去做這件事?用Android應用程序啓動應用程序或文件

謝謝。這就是我用你的例子工作。

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
      Uri data = Uri.parse("file://" + pathtofile); 
      intent.setDataAndType(data,"audio/mp3"); 
      try { 
         startActivity(intent); 
       } catch (ActivityNotFoundException e) { 
         e.printStackTrace(); 
       } 
+0

的[推出的Android使用意向的音樂播放器]可能重複(http://stackoverflow.com/questions/3114471/android-launching-music-player-using-intent) – 2011-01-13 19:52:19

回答

3

嘗試使用ACTION_VIEW:

Uri myResourceToPlay = new Uri(...); 
Intent playIntent = new Intent(Intent.ACTION_VIEW, myResourceToPlay); 
startActivity(playIntent); 
1

我經常搶啞劇和使用,推出的意圖時。

try { 
     //launch intent 
     Intent i = new Intent(Intent.ACTION_VIEW); 
     Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory())); 
     String url = uri.toString(); 

     //grab mime 
     String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
       MimeTypeMap.getFileExtensionFromUrl(url)); 

     i.setDataAndType(uri, newMimeType); 
     startActivity(i); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }