Playing sound from SDCard with MediaPlayer? - android

Let me explain clearly:
I have folder named "Sounds" and there are some .ogg files to play.
I got the folder path using Environment.getExternalStorageDirectory().getAbsolutePath() + mySoundsPath;.
I got all list from that folder and save it to array: List<String> soundList;
My question is:
How to call the sound from soundList that i created so they all can be played??
Should it need to decoded(like images, decoded to Bitmap)??
Sorry for my grammar.
Thanks in advance.

Use the below link for the reference
http://developer.android.com/reference/android/media/MediaPlayer.html
and you can try small code sample as well
http://davanum.wordpress.com/2007/12/29/android-videomusic-player-sample-from-local-disk-as-well-as-remote-urls/

public void play(String path) {
count++;
playFile=path;
//showNotification();
new NotificationPanel(activity);
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
Log.d("*****begin*****", "playing");
stopPlaying();
Log.d("*****begin*****", "stoping");
} else{
Log.d("*****begin*****", "nothing");
}
Uri myUri1 = Uri.parse(path);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(activity, myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mediaPlayer.start();
}
private void stopPlaying() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}

Related

Android Media Player does not stop. When I click on a play button, it plays multiple times

When I click on the play button again and again, it plays multiple times simultaneously. I want to stop multiple playing. Here's the code:
Objects for Media player and buttons
MediaPlayer mPlayer;
Button playbtn;
Button stopbtn;
Button click event listener to play audio
playbtn = (Button) findViewById(R.id.play);
playbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
Button click event listener to stop audio
stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
}
}
});
any help would be appreciated. Thanks!
It is creating a new player with every play click but only keeping the reference to the last player so the solution is to keep player in a list and stop all.
private List<MediaPlayer> players = new ArrayList<>()
stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
for(player in players){
if(player!=null && player.isPlaying()){
mPlayer.stop();
}
}
players.clear();
}
});
Another solution is, only use one instance of player. move the player initialization outside click as:
playbtn = (Button) findViewById(R.id.play);
Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
playbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!mPlayer.isPlaying())
mPlayer.start();

How can i save a song from my app in android? [duplicate]

This question already has answers here:
Downloading an mp3 file from server in android
(3 answers)
Closed 7 years ago.
i am unable to download the song from internet but i can listen the song but i want to my app to perform as, when i am clicking the play button it should download that song too.
playbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mPlayer = new MediaPlayer();
if(web[position] == web[0]){
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.prepareAsync();
mPlayer.seekTo(0);
}
else{
Uri myUri1 = Uri.parse("http://sound21.mp3slash.net/320/indian/bajrangibhaijaan/01%20-%20Selfie%20Le%20Le%20Re%20-%20Bajrangi%20Bhaijaan%20[Songspk.LINK].mp3");
try {
mPlayer.setDataSource(getContext(), myUri1);
} catch (IllegalArgumentException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
mPlayer.start();
playbtn.setText("Pause");
}
You mean when you click button , start playing and downloading?
Use DownloadManager to download the audio file.
http://developer.android.com/reference/android/app/DownloadManager.html

Streaming audio from website to app

I'm in the process of building a prototype for a project. I want to be able to stream music from my website to the app.
Here is what I have so far. Trying it with an m3u because I honestly have no idea what else to use for this.
Button buttonPlay;
Button buttonStop;
MediaPlayer mPlayer;
String url = "http://www.*******.com/*****/files.m3u";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Argument", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Security ", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Statement", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare Illegal State", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare IO", Toast.LENGTH_LONG).show();
}
mPlayer.start();
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
}
}
});
Them problem is it throws IOexception after prepare. Now I read somewhere you have to download m3u files and then read them line by line. If that is the case, is there a better way? I'm attempting to make sort of a radio app for testing purposes that will play songs at random, not in order, so I don't want to hard code them MP3 files,
you cant just prepare a stream because it needs to download asynchronously. you will need to do this
Button buttonPlay;
Button buttonStop;
MediaPlayer mPlayer;
String url = "http://www.*******.com/*****/files.m3u";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Argument", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Security ", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Statement", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepareAsync();
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
}
};);
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare Illegal State", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare IO", Toast.LENGTH_LONG).show();
}
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){
mPlayer.stop();
}
}
});

Android: won't play rtp stream

I want to make an app which plays the sound from an server.
Receiving and playing from the internet works, but with my JMF-Programm it does not work (Error (-38,0)).
here is my code-snippet from the app:
MediaPlayer mPlayer = new MediaPlayer();
// String url
// ="http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3";
String url = "rtp://192.168.32.29:22222/audio/16";
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
AudioManager audioManager = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, volume);
try {
mPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
} catch (SecurityException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),
"You might not set the URI correctly!", Toast.LENGTH_LONG)
.show();
}
mPlayer.start();
when I use "http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3" it works, but "rtp://192.168.32.29:22222/audio/16" sends me error (-38,0)
Can someone help me?
Thanks

Playing video in android

I have 4 .mp4 videos that I need to play in an android app. I managed to play 1 video, but the other 3 won't play. I think video size is the issue. The video that I managed to play has a size of 1.4mb and the other 3 have a size of 6mb, 2.2mb, 3.8mb.
Here's my code for playing them.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoviewer);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("VidName");
if(value.equals("MinorBurnVid"))
{
try
{
videoView1 = (VideoView)findViewById(R.id.Video1);
videoView1.setVideoPath("android.resource://dr.droid/" + R.raw.burn);
videoView1.setMediaController(new MediaController(this));
videoView1.requestFocus();
videoView1.start();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
else if(value.equals("ChokingAdultVid"))
{
try
{
videoView1 = (VideoView)findViewById(R.id.Video1);
videoView1.setVideoPath("android.resource://dr.droid/" + R.raw.chokingadult);
videoView1.setMediaController(new MediaController(this));
videoView1.requestFocus();
videoView1.start();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
else if(value.equals("CPRAdultVid"))
{
try
{
videoView1 = (VideoView)findViewById(R.id.Video1);
videoView1.setVideoPath("android.resource://dr.droid/" + R.raw.cpr);
videoView1.setMediaController(new MediaController(this));
videoView1.requestFocus();
videoView1.start();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
else if(value.equals("Fracture"))
{
try
{
videoView1 = (VideoView)findViewById(R.id.Video1);
videoView1.setVideoPath("android.resource://dr.droid/" + R.raw.fracture);
videoView1.setMediaController(new MediaController(this));
videoView1.requestFocus();
videoView1.start();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}
}
The burn video is the one that I managed to play but the other 3, no luck! Any ideas?
Are you getting an exception or is it just ignoring the code snippet? If there are no exceptions make sure the string stored in "value" is correct, it could be a minor problem like being case-sensitive. Hopefully.
String uri = "android.resource://dr.droid/" + R.raw.fracture;
videoView1.setVideoURI(Uri.parse(uri));

Categories

Resources