package com.appli.gui;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.appli.patoche.R;

import java.io.IOException;

public class player_mp3 extends Activity
{
    Button buttonStop,buttonStart ;
    //String AudioURL = "https://www.android-examples.com/wp-content/uploads/2016/04/Thunder-rumble.mp3";
    String AudioURL = "http://analog-design.net/WORKAREA/WEBSITE/Codebar/";
    MediaPlayer mediaplayer;
    String url_mp3;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.player_mp3_layout);

        TextView txtProduct = (TextView) findViewById(R.id.mp3_titre);

        Intent i = getIntent();
        String mp3_titre = i.getStringExtra("mp3_titre");
        txtProduct.setText(mp3_titre);

        buttonStart = (Button)findViewById(R.id.button1);
        buttonStop = (Button)findViewById(R.id.button2);

        mediaplayer = new MediaPlayer();
        mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        url_mp3 = AudioURL + mp3_titre;

        buttonStart.setOnClickListener(new View.OnClickListener()



        {
            @Override
            public void onClick(View v)
            {

                try
                {

                    String filePath = Environment.getExternalStorageDirectory()+"/Download/Enigma Obscura.mp3";

                    mediaplayer.setDataSource(filePath);
                    mediaplayer.prepare();

                    // avec url
                    //mediaplayer.setDataSource(url_mp3);
                    //mediaplayer.prepare();
                }

                catch (IOException e)
                {
                    e.printStackTrace();
                }

                catch (IllegalArgumentException e)
                {
                    e.printStackTrace();
                }

                catch (SecurityException e)
                {
                    e.printStackTrace();
                }

                catch (IllegalStateException e)
                {
                    e.printStackTrace();
                }

                mediaplayer.start();

            }
        });

        buttonStop.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mediaplayer.stop();
            }
        });


    }
}