2010-06-24 40 views

Trả lời

79

Tôi đã tìm thấy một cách để thực hiện việc này.

Intent intent = new Intent(); 
intent.setAction(android.content.Intent.ACTION_VIEW); 
File file = new File(YOUR_SONG_URI); 
intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
startActivity(intent); 
+0

công trình như một say mê :) – Calvin

+0

Làm thế nào để kiểm tra các media player là ứng dụng là availbe hay không trên thiết bị –

+0

này không hoạt động trên Nexus 6 (mở trong ảnh ứng dụng) – nbarraille

19

Để đơn giản khởi động máy nghe nhạc làm:

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER); 
startActivity(intent); 
+1

xin lưu ý rằng điều này chỉ hoạt động trong SDK API cấp 8. – grzaks

+0

plz bạn sẽ cho tôi biết tất cả mọi thứ nên được thực hiện sau khi viết mã trên của bạn. Trợ giúp của bạn sẽ thực sự mang lại lợi ích cho tôi và những người khác –

+4

Lưu ý rằng hành động này đã bị phản đối vì lợi ích của CATEGORY_APP_MUSIC –

0

Bạn cũng có thể thử thế này.

Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 
2
  if (Build.VERSION.SDK_INT >= 24) { 
       try { 
        Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure"); 
        m.invoke(null); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      Intent intent = new Intent(); 
      intent.setAction(android.content.Intent.ACTION_VIEW); 
      File file = new File(YOUR_SONG_URI); 
      intent.setDataAndType(Uri.fromFile(file), "audio/*"); 
      startActivity(intent); 
0

Có số cách mà bạn có thể đạt được trình nghe nhạc mặc định nhưng đó là những thiết bị và hệ điều hành cụ thể.

Với đoạn mã này, bạn có thể nhận trình phát âm thanh mặc định.

try 
    { 
    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW); 
    File file = new File("audiofilepath"); 
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()); 
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 
    myIntent.setDataAndType(Uri.fromFile(file),mimetype); 
    startActivity(myIntent); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
Các vấn đề liên quan