繁体   English   中英

如何避免音频播放器中不受支持的格式

[英]how to avoid unsupported formats in audio player

我正在创建一个音频播放器,并且正在使用此代码来获取所有歌曲

String[] STAR = {"*"};

    Cursor cursor;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

      File file;

    cursor =context.getContentResolver().query(uri, STAR, selection, null, null);

    if (cursor != null) {

        if (cursor.moveToFirst()) {
            int i = 0;
            do {



                String songName = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));

                path[i] = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));


                String albumName = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                albumId[i] = cursor
                        .getInt(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                String artist= cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String albumname= cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                artistId[i]=cursor.getInt(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));



        }cursor.close();

这使我得到了歌曲,但我也得到了不受支持的格式的音频。 如.wav等,如何避免不支持的格式

您可以使用MimeType执行此操作。 请参阅下面的示例代码,如何针对mp3完成此操作。

ContentResolver contentResolver = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = null;
String sortOrder = null;
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = imeTypeMap.getSingleton().getMimeTypeFromExtension("mp3"); 
String[] selectionArgsMp3 = new String[]{ mimeType };
Cursor mp3Songs = contentResolver.query(uri, projection,selectionMimeType,selectionArgsmp3, sortOrder);

您可以检查的另一种方法是在打开/播放应用程序时使用以下代码在设备上安装了任何兼容的应用程序。

private void openRelevantFileHandler(String pathToMusicFile)
{
    String extension = MimeTypeMap.getFileExtensionFromUrl(pathToMusicFile);
    String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("file://" + pathToMusicFile), type);

    PackageManager packageManager = context.getPackageManager();
    List activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL);
    boolean isIntentSafe = activities.size() > 0;
    if (!isIntentSafe) {
        Utility.showToast(context, "No application available on your device to open this type of file.");
    }
    else 
    {
        context.startActivity(intent);//Start related application
    }
}

希望这会在某种程度上帮助您:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM