Android MP3 Player, Play MP3 From Assets, Raw, Online, SDCard and Using Intent

Screenshot_2015-03-08-16-54-52Android MP3 Player, Play MP3 From Assets, Raw, Online, SDCard and Using Intent

MainActivity.java :

package com.player;

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.pop);
		// 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;
    }

}

activity_main.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btnmp3raw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="Play Mp3 Raw" />

    <Button
        android:id="@+id/btnmp3assets"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="Play Mp3 Assets" />

    <Button
        android:id="@+id/btnmp3online"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="Play Mp3 Online" />

    <Button
        android:id="@+id/btnmp3sdcard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="Play Mp3 SdCard" />

    <Button
        android:id="@+id/btnmp3intentsdcard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="Play Mp3 Intent SdCard" />

    <Button
        android:id="@+id/btnmp3intentonline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:text="Play Mp3 Intent Online" />

</LinearLayout>

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.player"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.player.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Result :

Screenshot_2015-03-08-16-54-52

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>