Android中的Mp3 /音乐播放器

时间:2016-09-20 13:07:47

标签: android media-player audio-player android-music-player

我正在尝试在Android中创建一个音乐播放器。此代码用于读取mp3文件的SD卡并以吐司形式显示。

当我在手机上运行此应用程序时,手机会显示一个空白屏幕,即不显示任何歌曲。

var beaconsArray = Object.values(beacons);

1 个答案:

答案 0 :(得分:1)

这是正确的答案:

public class MainActivity extends AppCompatActivity {

TextView countTextview;
ListView listView;
private List<String> arrayList;
ArrayAdapter arrayAdapter;
final private static int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    countTextview = (TextView)findViewById(R.id.count);

    arrayList = new ArrayList<String>();

    listView = (ListView)findViewById(R.id.listView); 

    arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);

    permissionManager();

}

public void add(){

    ContentResolver cr = getContentResolver();

    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
    Cursor cur = cr.query(uri, null, selection, null, sortOrder);
    int count = 0;

    if(cur != null)
    {
        count = cur.getCount();

        if(count > 0)
        {
            while(cur.moveToNext())
            {
                String data = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));

                arrayList.add(data);

            }

            listView.setAdapter(arrayAdapter);

        }
    }

    cur.close();

}


public void permissionManager(){

    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }else {
        add();

    }

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                add();


            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.

                Toast.makeText(MainActivity.this, "Permission is really required to get Songs", Toast.LENGTH_SHORT).show();
                permissionManager();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

}