2011-01-13 181 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(); 
    }