Пример #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Song);

            Songs  songs     = new Songs();
            int    songIndex = Intent.GetIntExtra("songIndex", 0);
            string song      = songs.GetSong(songIndex);

            StartSong(song);

            ImageButton backButton   = (ImageButton)FindViewById <ImageButton>(Resource.Id.back);
            ImageButton prevButton   = (ImageButton)FindViewById <ImageButton>(Resource.Id.prevSong);
            ImageButton playButton   = (ImageButton)FindViewById <ImageButton>(Resource.Id.play);
            ImageButton nextButton   = (ImageButton)FindViewById <ImageButton>(Resource.Id.nextSong);
            ImageButton stopButton   = (ImageButton)FindViewById <ImageButton>(Resource.Id.stop);
            ImageButton pauseButton  = (ImageButton)FindViewById <ImageButton>(Resource.Id.pause);
            ImageButton recordButton = (ImageButton)FindViewById <ImageButton>(Resource.Id.record);

            prevButton.LongClick += delegate
            {
                StartSong(songs.GetSong(0));
            };

            prevButton.Click += delegate
            {
                if (songIndex != 0)
                {
                    song       = songs.GetSong(songIndex - 1);
                    songIndex -= 1;
                    StartSong(song);
                }
            };

            playButton.Click += delegate
            {
                if (isPaused)
                {
                    mediaPlayer.Start();
                }
                if (isStopped)
                {
                    StartSong(songs.GetSong(songIndex));
                }
                isPaused  = false;
                isStopped = false;
            };

            nextButton.Click += delegate
            {
                if (songIndex != 2)
                {
                    song       = songs.GetSong(songIndex + 1);
                    songIndex += 1;
                    StartSong(song);
                }
            };

            nextButton.LongClick += delegate
            {
                StartSong(songs.GetSong(2));
            };

            stopButton.Click += delegate
            {
                if (mediaPlayer.IsPlaying)
                {
                    mediaPlayer.Stop();
                }
                isStopped = true;
            };

            pauseButton.Click += delegate
            {
                if (mediaPlayer.IsPlaying)
                {
                    mediaPlayer.Pause();
                }
                isPaused = true;
            };

            recordButton.Click += delegate
            {
                if (mediaPlayer.IsPlaying)
                {
                    if (!isRecording)
                    {
                        recordButton.BackgroundTintList = GetColorStateList(Resource.Color.red);
                        mediaRecorder = new MediaRecorder();
                        isRecording   = true;
                        mediaRecorder.SetAudioSource(AudioSource.Mic);
                        mediaRecorder.SetOutputFormat(OutputFormat.ThreeGpp);
                        mediaRecorder.SetOutputFile(Android.OS.Environment.ExternalStorageDirectory + "/" + song + ".3gp");
                        mediaRecorder.SetAudioEncoder(AudioEncoder.AmrNb);
                        mediaRecorder.Prepare();
                        mediaRecorder.Start();
                    }
                    else
                    {
                        isRecording = false;

                        try
                        {
                            mediaRecorder.Stop();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }

                        mediaRecorder.Release();
                        mediaRecorder = null;
                    }
                }
            };

            backButton.Click += delegate
            {
                mediaPlayer.Release();
                StartActivity(new Intent(this, typeof(MainActivity)));
            };
        }