package com.example.player02;

import java.io.File;
import java.io.IOException;
 
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
    Button btnmp3raw;
    Button btnmp3assets;
    Button btnmp3online;
    Button btnmp3sdcard;
    Button btnmp3intentsdcard;
    Button btnmp3intentonline;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Button btnmp3raw = (Button) findViewById (R.id.btnmp3raw);
        Button btnmp3assets = (Button) findViewById (R.id.btnmp3assets);
        Button btnmp3online = (Button) findViewById (R.id.btnmp3online);
        Button btnmp3sdcard = (Button) findViewById (R.id.btnmp3sdcard);
        Button btnmp3intentsdcard = (Button) findViewById (R.id.btnmp3intentsdcard);
        Button btnmp3intentonline = (Button) findViewById (R.id.btnmp3intentonline);
 
        btnmp3raw.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                playmp3raw();
            }
        });
 
        btnmp3assets.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                playmp3assets("pop.mp3");
            }
        });
 
        btnmp3online.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
        playmp3online();
            }
        });
 
        btnmp3sdcard.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                playmp3sdcard();
            }
        });
 
        btnmp3intentsdcard.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setAction(android.content.Intent.ACTION_VIEW);
                // Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
                File file = new File(android.os.Environment.getExternalStorageDirectory() + "/pop.mp3");
                intent.setDataAndType(Uri.fromFile(file), "audio/*");
                startActivity(intent);
            }
        });
 
        btnmp3intentonline.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Uri myUri = Uri.parse("http://192.168.43.183/pop.mp3");
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
                intent.setDataAndType(myUri, "audio/*");
                startActivity(intent);
            }
        });
 
    }
 
    public void playmp3assets(String soundfile) {
        MediaPlayer m;
        try {
            m = new MediaPlayer();
            // AssetManager mngr = getAssets();
            if (m.isPlaying()) {
                m.stop();
                m.release();
                m = new MediaPlayer();
            }
 
            // AssetFileDescriptor afd = getAssets().openFd(fileName);
            AssetFileDescriptor descriptor = getAssets().openFd(soundfile);
            m.setDataSource(descriptor.getFileDescriptor(),
                    descriptor.getStartOffset(), descriptor.getLength());
            descriptor.close();
 
            m.prepare();
            m.setVolume(1f, 1f);
            // m.setLooping(true);
            m.start();
        } catch (Exception e) {
        }
 
    }
 
    public void playmp3raw() {
        MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.musique);
        // dynamic mp3 name
        // MediaPlayer mp = MediaPlayer.create(MainActivity.this, Uri.parse("android.resource://" + getPackageName() + "/raw/pop"));
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub
//              Button01.setEnabled(true);
//              TextView01.setText("Play");
            }
        });
    }
 
    public void playmp3sdcard() {
        File filemp3 = new File(android.os.Environment.getExternalStorageDirectory() + "/pop.mp3");
        //MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.pop);
        MediaPlayer mp = MediaPlayer.create(MainActivity.this, Uri.fromFile(filemp3));
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub
//              Button01.setEnabled(true);
//              TextView01.setText("Play");
            }
        });
    }
    public void playmp3online(){
        try {
            MediaPlayer player = new MediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource("http://192.168.43.183/pop.mp3"
                    );
            player.prepare();
                        player.start();
 
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    
/*    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
*/        
    }
 
