Does any one have code they know will run android audio stream from a URL, that will work in android OS 2.2. I have looked on the developer site and there code doesn't seem to work for me. Some of the other code or tutorials are for older versions of android.
This was the code from the code i ve been using(won't work):
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Radio extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
final MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(URL);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// This is code for a button that starts the stream when clicked
Button bRadio = (Button) findViewById(R.id.button_stream);
bRadio.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View V) {
mp.start();
}
});
}
}
As you are setting datasource to URL, use mp.prepareAsync() instead of mp.prepare().
if you are ok with JNI, there is some sample code here:
the creating JNI URI player is liking this:
// configure audio source
SLDataLocator_URI loc_uri = {SL_DATALOCATOR_URI, (SLchar *) uri_utf8};
SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};
SLDataSource audioSrc = {&loc_uri, &format_mime};
// configure audio sink
SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
SLDataSink audioSnk = {&loc_outmix, NULL};
// create audio player
const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO, SL_IID_VOLUME};
const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
(*engineEngine)->CreateAudioPlayer(engineEngine, &uriPlayerObject, &audioSrc,&audioSnk, 3, ids, req);
(*uriPlayerObject)->Realize(uriPlayerObject, SL_BOOLEAN_FALSE);
(*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_PLAY, &uriPlayerPlay);
... get other interfaces...
then you could play:
(*uriPlayerPlay)->SetPlayState(uriPlayerPlay,SL_PLAYSTATE_PLAYING);
Related
I searched the google and even the stackoverflow for the solution, but nothing worked and this is really making me insane now. :(
I want the only item to be highlighted is the one which I click on the listview. When I click on the item, it gets highlighted in red color. But once I scroll the list, I find multiple items are getting highlighted with red, and coming back to the item I find it turned to default color, that is non-highlighted mode. Getting crazy over this, working on this from the past 3 hours still unable to find a solution. :( Would really appreciate your advice. I know this is my third post in a day but I am quite new to android, so stumbling over this a few times. My code is stated below-
package com.example.mp3;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener, OnCompletionListener {
ListView list;
ArrayAdapter<String> listAdapter ;
ArrayList<String> listTest;
ArrayList<String> listSoundNames;
ImageButton play,stop,back,next;
String songpath,song,title;
int index,current_position;
File[] listFile;
SharedPreferences sharedPref;
MediaPlayer mp,mp2;
ActionBar bar;
private Boolean state=false;
private static int save = -1;
int count=0;
private static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
song = sharedPref.getString("songname", "name");
mp=new MediaPlayer();
mp2 = new MediaPlayer();
mp.setOnCompletionListener(this);
list = (ListView)findViewById(R.id.list);
//list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listTest = new ArrayList<String>( );
listSoundNames=new ArrayList<String>();
play = (ImageButton)findViewById(R.id.play);
back = (ImageButton)findViewById(R.id.prev);
next = (ImageButton)findViewById(R.id.next);
//adding listeners
play.setOnClickListener(this);
back.setOnClickListener(this);
next.setOnClickListener(this);
//action bar controls
bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF0174")));
Scanner("/sdcard/");///storage path
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////*Adding listener to songs*//////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(listTest.size() != 0)
{
listAdapter = new ArrayAdapter<String> (MainActivity.this,R.layout.simplerow, listSoundNames);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
///////////////////changing list item background on click///////////////////
//view.setSelected(true); ///////////////////PROBLEM/////////////
for(int a = 0; a < parent.getChildCount(); a++)
{
parent.getChildAt(a).setBackgroundColor(Color.BLACK);
}
view.setBackgroundColor(Color.RED);
////////////////////////////////////////////////////////////////////////////
//accessing song path
String selected = listTest.get(position);
list.setItemChecked(position, true);//
//accessing the song name
String name = (String) ((TextView) view).getText();
title = name;
//bar.setTitle(title);
//Log.e(TAG, name);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
try{
mp.reset();
mp.setDataSource(listTest.get(position));//source
mp.prepare();
mp.start();
index = position;
play.setImageResource(R.drawable.pause);
}
catch(Exception e){e.printStackTrace();}
}
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////*Songs added here to list*////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
private void Scanner(String path) {
// TODO Auto-generated method stub
{
try
{
File fl = new File(path);
File[] listOfFiles = fl.listFiles();
for (File listOfFile : listOfFiles)
{
String s = listOfFile.getName();
if(s.endsWith(".mp3"))
{
songpath = listOfFile.getPath();
listTest.add(songpath);//adding song names to list
//listTest.toString().replaceFirst(songpath, s);
// store file name in listSoundNames
int pos = s.lastIndexOf(".");
if (pos > 0)
{
song = s.substring(0, pos);
}
listSoundNames.add(song);
}
/////////////////////////////////
File f = new File(path+s+"/");
if (f.exists() && f.isDirectory()) {
Scanner(path+s+"/");
}
////////////////////////////////
}
}
catch (Exception e) { }
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(play))
{
if(mp.isPlaying())
{
mp.pause();
Toast.makeText(MainActivity.this, "paused", Toast.LENGTH_SHORT).show();
//change in button image//
play.setImageResource(R.drawable.play);
}
else
{
mp.start();
Toast.makeText(MainActivity.this, "started", Toast.LENGTH_SHORT).show();
//change in button image//
play.setImageResource(R.drawable.pause);
//
}
}
if (v.equals(back))
{
mp.stop();
mp.reset();
//bar.setTitle(song);
if(index!=0)
{
index = index -1;
}
else
{
index = (list.getAdapter().getCount()-1)-1;
}
Uri uri = Uri.parse(listTest.get(index).toString());//getting the path of next song
try {
mp.setDataSource(getApplicationContext(), uri);//setting new data source
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();///PROBLEM:MOVING HERE AFTER CLICKING NEXT BUTTON
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();//PROBLEM: NOT PLAYING
Toast.makeText(MainActivity.this, ""+uri, Toast.LENGTH_SHORT).show();
}
if (v.equals(next))
{
mp.stop();
mp.reset();
index = index +1;
Uri uri = Uri.parse(listTest.get(index).toString());//getting the path of next song
try {
mp.setDataSource(getApplicationContext(), uri);//setting new data source
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();///PROBLEM:MOVING HERE AFTER CLICKING NEXT BUTTON
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();//PROBLEM: NOT PLAYING
Toast.makeText(MainActivity.this, ""+uri, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
play.setImageResource(R.drawable.play);
}
/*#Override
protected void onStop() {
super.onStop();
mp.stop();
Toast.makeText(getApplicationContext(), "stopped", Toast.LENGTH_LONG).show();
}*/
}
I have a problem with my soundboard im quite new to programming and i need help from some pros
The Problem is that my soundboard doesnt stop when i press the home button or the return button i need it to pause the sound or stop it here is the code hope you can help
package com.example.firstly;
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Mymenu extends Activity {
int selectedSoundId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final MediaPlayer player = new MediaPlayer();
final Resources res = getResources();
//just keep them in the same order, e.g. button01 is tied to backtoyou
final int[] buttonIds = { R.id.dinal, R.id.ele, R.id.syl,
R.id.amel, R.id.krz, R.id.mar,
R.id.sra, R.id.bab, R.id.har,
R.id.kur, };
final int[] soundIds = { R.raw.dinal_ama, R.raw.daj_ama, R.raw.syl_ama,
R.raw.ame_ama, R.raw.krzy_ama, R.raw.marihuanen_ama,
R.raw.srac_ama, R.raw.zajeb_ama, R.raw.hardcore_ama,
R.raw.oookurwa_ama, };
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
//find the index that matches the button's ID, and then reset
//the MediaPlayer instance, set the data source to the corresponding
//sound effect, prepare it, and start it playing.
for(int i = 0; i < buttonIds.length; i++) {
if(v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
break;
}
}
}
};
//set the same listener for every button ID, no need
//to keep a reference to every button
for(int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button)findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener); }
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
Stop or pause the player in your activity's on pause, use the code below
package com.example.firstly;
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Mymenu extends Activity {
int selectedSoundId;
MediaPlayer player;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
player = new MediaPlayer();
final Resources res = getResources();
// just keep them in the same order, e.g. button01 is tied to backtoyou
final int[] buttonIds = { R.id.dinal, R.id.ele, R.id.syl, R.id.amel,
R.id.krz, R.id.mar, R.id.sra, R.id.bab, R.id.har, R.id.kur, };
final int[] soundIds = { R.raw.dinal_ama, R.raw.daj_ama, R.raw.syl_ama,
R.raw.ame_ama, R.raw.krzy_ama, R.raw.marihuanen_ama,
R.raw.srac_ama, R.raw.zajeb_ama, R.raw.hardcore_ama,
R.raw.oookurwa_ama, };
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
// find the index that matches the button's ID, and then reset
// the MediaPlayer instance, set the data source to the
// corresponding
// sound effect, prepare it, and start it playing.
for (int i = 0; i < buttonIds.length; i++) {
if (v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res
.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
break;
}
}
}
};
// set the same listener for every button ID, no need
// to keep a reference to every button
for (int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button) findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
player.stop(); // to stop the player
player.release(); // if you want to pause the song use player.pause();
}
}
I don't know more what I can do to solve these problems. I am
implementing a MediaPlayer + Equalizer app. My MediaPlayer works fine,
however, I am having problem with my equalizer: some its methods don't
work.
public class MainActivity extends Activity {
private MediaPlayer mediaPlayer;
private Equalizer equalizer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("/sdcard/titanium.mp3");
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPlayer.start();
equalizer = new Equalizer(0, 0);
equalizer.setEnabled(true);
int minEQLevel = equalizer.getBandLevelRange()[0];
int maxEQLevel = equalizer.getBandLevelRange()[1];
Log.i("kkk", "Valor de minEQLevel" + minEQLevel);
Log.i("kkk", "Valor de maxEQLevel" + maxEQLevel);
short numberOfBand = equalizer.getNumberOfBands();
Log.i("kkk", "NĂºmero de bandas = "+numberOfBand);
/******From here to down the methods don't work *****/
try {
short band = equalizer.getBand(Integer.valueOf(50000));
Log.i("kkk", "getBand() = "+band);
short band = 0;
//int menor = equalizer.getBandFreqRange(band)[0];
//int maior = equalizer.getBandFreqRange(band)[1];
//Log.i("kkk", "Menor = "+menor);
//Log.i("kkk", "Maior = "+maior);
} catch (IllegalArgumentException e) {
Log.i("kkk", "Bad parameter value");
} catch (UnsupportedOperationException e) {
Log.i("kkk", "get parameter() rejected");
} catch (IllegalStateException e) {
Log.i("kkk", "get parameter() called in wrong state");
}
}
}
The follow methods work normally:
getBandLevelRange();
getNumberOfBands();
The follow methods don't work. I get the message error: Bad parameter
value.
getBand();
getBandFreqRange(); this last is commented, but when it isn't also don't work.
In the getBand() method I already tried several parameters values like: 50000, 1000000. Frequency supported at example of the library AudioFx.
Why don't these methods work??? Did I forgive to setup anything???
tks.
I am trying to play an url but its not playing and the code i used is below..the logcat is showing Mediaplayer error(1,-1002), start state is 0 and error(-38, 0) why...? where i am going wrong......can u help me out how to play........
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class BacaFatihahActivity extends Activity {
final String songs_urIs= "http://stream.radiosai.net:8002/";
// private TextView txt_song_title;
private MediaPlayer mediaplayer;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton btn_play = (ImageButton) findViewById(R.id.button_play);
ImageButton btn_pause = (ImageButton) findViewById(R.id.button_pause);
ImageButton btn_next = (ImageButton) findViewById(R.id.button_next);
ImageButton btn_previous = (ImageButton) findViewById(R.id.button_Previous);
//txt_song_title = (TextView) findViewById(R.id.txt_song_title);
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
btn_play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
mediaplayer.setDataSource(songs_urIs);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaplayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaplayer.start();
}
});
}
}
right code but wrong api level~
it's not every api level supports this way to play a media,http live streaming ,may be you need api level 10 or higher
I'm currently have a media player that is streaming an mp3 file. When that file is done, what is the code so that it goes to the next url/mp3?
And also, is there code to get the name of the file and display it? how would I go about doing that?
thanks
EDIT
see my code below:
package com.example.m3uplayer;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.media.MediaPlayer.OnCompletionListener;
public class m3uPlayer extends Activity implements MediaPlayer.OnCompletionListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
//http://dl.dropbox.com/u/24535120/AfroJack-UMF11-clip2.mp3
//http://dl.dropbox.com/u/24535120/Avicii%20clip%201.mp3
Uri myUri = Uri.parse("http://dl.dropbox.com/u/24535120/AfroJack-UMF11-clip2.mp3");
MediaPlayer sdrPlayer = new MediaPlayer();
try {
sdrPlayer.setDataSource(this, myUri);//"http://mp1.somafm.com:8032");
sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
sdrPlayer.prepare(); //don't use prepareAsync for mp3 playback
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sdrPlayer.start();
}
#Override
public void onCompletion(MediaPlayer sdrPlayer) {
Uri myUri5 = Uri.parse("http://dl.dropbox.com/u/24535120/Avicii%20clip%201.mp3");
try {
sdrPlayer.setDataSource(this, myUri5);
sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
sdrPlayer.prepare(); //don't use prepareAsync for mp3 playback
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sdrPlayer.start();
}
}
You can register an OnCompletionListener with the media player. When it receives its callback notification, you need to call reset(), setDataSource, and prepare() on the media player for the next URL.
I don't believe there is anything in the api to tell you what data source a media player is using. You need to keep track of that yourself.