Ejemplo n.º 1
0
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            if (requestCode == PERMISSION_ReadExternalStorage)
            {
                if (grantResults[0] == Permission.Granted)
                {
                    Songleton.Instance.AllSongs = MusicRetrieval.LoadSongs(this);
                    SongListFragment songListFragment = new SongListFragment();
                    //MusicControlFragment musicControlFragment = new MusicControlFragment ();
                    MusicControls musicControls = new MusicControls();
                    //var trans = FragmentManager.BeginTransaction ();以下两条语句极度重要,解决大问题!
                    Android.Support.V4.App.FragmentManager     fragmentManager = SupportFragmentManager;
                    Android.Support.V4.App.FragmentTransaction trans           = fragmentManager.BeginTransaction();
                    trans.Add(Resource.Id.fragment_container, songListFragment, "songListFragment");
                    trans.Add(Resource.Id.music_controls_container, musicControls, "musicControls");
                    trans.Commit();

                    if (Build.VERSION.SdkInt != Android.OS.BuildVersionCodes.NMr1)
                    {
                        Toast.MakeText(this, "读取数据权限已申请", ToastLength.Short).Show();
                    }
                }
                else
                {
                    if (Build.VERSION.SdkInt != Android.OS.BuildVersionCodes.NMr1)
                    {
                        Toast.MakeText(this, "申请读取数据权限被拒", ToastLength.Short).Show();
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public void OnPrepared(MediaPlayer mp)
 {
     player.Start();
     MusicControls.songName.Text     = Songleton.Instance[SongPos].Title;
     MusicControls.songDuration.Text = getTime(player.Duration);
     MusicControls.ThreadHandler(0);
 }
Ejemplo n.º 3
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            //java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=17271, uid=10085 requires android.permission.READ_EXTERNAL_STORAGE
            //6.0及以上需要动态申请权限
            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
            {
                //RequestWriteSettings();
                bool isGrant = ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == Permission.Granted;
                if (isGrant == false)
                {
                    ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, PERMISSION_ReadExternalStorage);
                }
                else
                {
                    Songleton.Instance.AllSongs = MusicRetrieval.LoadSongs(this);
                    if (savedInstanceState == null)
                    {
                        SongListFragment songListFragment = new SongListFragment();
                        //MusicControlFragment musicControlFragment = new MusicControlFragment ();
                        MusicControls musicControls = new MusicControls();
                        //var trans = FragmentManager.BeginTransaction ();以下两条语句极度重要,解决大问题!
                        Android.Support.V4.App.FragmentManager     fragmentManager = SupportFragmentManager;
                        Android.Support.V4.App.FragmentTransaction trans           = fragmentManager.BeginTransaction();
                        trans.Add(Resource.Id.fragment_container, songListFragment, "songListFragment");
                        trans.Add(Resource.Id.music_controls_container, musicControls, "musicControls");
                        trans.Commit();
                    }
                    //在程序中获取string.xml中字符串和数值
                    Toast.MakeText(this, this.Resources.GetString(Resource.String.scan_songs) + Songleton.Instance.AllSongs.Count.ToString(), ToastLength.Short).Show();
                }
            }
        }
Ejemplo n.º 4
0
        public void PlaySong()
        {
            if (MusicControls.musicThread.IsAlive)
            {
                MusicControls.ThreadHandler(1);
            }
            player.Reset();
            //是因为以下没在主页展示,所以调用时为空指针,报错。先生成再播放
            MusicControls.imgPlay.SetImageResource(MusicControls.PAUSE_RESOURCE);
            var trackUri = ContentUris.WithAppendedId(MediaStore.Audio.Media.ExternalContentUri, Songleton.Instance[SongPos].Id);

            try {
                player.SetDataSource(Application.Context, trackUri);
            } catch (Exception e) {
                Log.Error(MainActivity.MUSIC_ERR_TAG, "Error setting data source", e);
            }
            player.PrepareAsync();
        }