Android-简单播放器——播放( 应用程序、内存卡以及网络音乐)


效果如图:

Android-简单播放器——播放( 应用程序、内存卡以及网络音乐)_第1张图片

MainActivity
1
2
3
4
5
6
7
8
9
10
11
12
package zking.com.g150825_andrioid_playmusic;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private ImageButton imageButton;
    private MediaPlayer mediaPlayer;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.seekbarplay);
        imageButton = (ImageButton) findViewById(R.id.playorpause);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,Musicpool.class);
                startActivity(intent);
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

                int pro=seekBar.getProgress();
                mediaPlayer.seekTo(pro);
            }
        });


        imageButton.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                final ImageButton imageButton= (ImageButton) view;


                if(mediaPlayer==null){

//                    播放应用程序的音乐
                   mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.huanqin);
                    mediaPlayer.start();

//  播放内存卡中的音乐
//                    mediaPlayer=new MediaPlayer();
//设置音源
//                    try {
//                        //mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/qqmusic/song/22.mp3"));
//                    网络音乐
//                        mediaPlayer.setDataSource(this, Uri.parse("http://193.168.4.135/internet.mp3"));
//
//                       准备
//                mediaPlayer.prepare();
//
//                        mediaPlayer.prepareAsync();
//
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }

                    imageButton.setImageResource(android.R.drawable.ic_media_pause);
//                    设置准备完成的监听
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {

                            mediaPlayer.start();
                            imageButton.setImageResource(android.R.drawable.ic_media_pause);
                            //获取音乐的时长
                            int dur=mediaPlayer.getDuration();
                            //将进度条设置最大值为音乐的时长
                            seekBar.setMax(dur);
                            new mythread().start();
                        }
                    });


                }else if(mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                    imageButton.setImageResource(android.R.drawable.ic_media_play);

                }else{
                    mediaPlayer.start();
                    imageButton.setImageResource(android.R.drawable.ic_media_pause);

                }

            }
        });


    }

    class mythread extends Thread{
        @Override
        public void run() {
            super.run();
            while(seekBar.getProgress()<=seekBar.getMax()){
                //获取音乐当前播放的位置
                int position=mediaPlayer.getCurrentPosition();
                seekBar.setProgress(position);
            }
        }
    }
}
 
          

activity_main

1
2
3
4
5
6
7
8
9
10
11
12
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context="zking.com.g150825_andrioid_playmusic.MainActivity">


    <SeekBar
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/seekbarplay"
        />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_play"
        android:id="@+id/playorpause"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="音乐池"
        android:id="@+id/button"
        />
    LinearLayout>

 
          








你可能感兴趣的:(MediaPlayer,Android)