I tried to implement bassboost effect in android..but there is no effect..I used the following code for bassboost effect;(I tried using global audio session id and i added permissions in manifest file)-->modify audio settings..
enter code here
BassBoost boost = new BassBoost(0,mp.getAudioSessionId());
boost.setEnabled(true);
boost.setStrength((Short)1000);
mp.attachAuxEffect(boost.getId());--->if i use this i'm getting(-38,0) or else no effect
mp.setSendLevel(1.0f);
I used below code for bass boost effect.got media player(-38,0) error..please resolve this issue..
enter code here
public class MainActivity extends Activity
{
MediaPlayer mediaplayer;
AssetFileDescriptor descriptor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_main);
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
descriptor = MainActivity.this.getAssets().openFd("Kalimba.mp3");
} catch (IOException e1) {
e1.printStackTrace();
}
try {
mediaplayer.setDataSource(descriptor.getFileDescriptor(),
descriptor.getStartOffset(), descriptor.getLength());
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
descriptor.close();
} catch (IOException e1) {
e1.printStackTrace();
}
setUpBooster();
mediaplayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
public void setUpBooster() {
BassBoost booster = new BassBoost(0,0);//-->tried with sessionid also
booster.setStrength((short) 1000);
booster.setEnabled(true);
mediaplayer.attachAuxEffect(booster.getId());
mediaplayer.setAuxEffectSendLevel(1.0f);
try {
mediaplayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
error 38 means you are asking the MediaPlayer to do something when in the wrong state. You won't be able to fathom your error just from this.
And anytime you call setDataSource() on it you will need to prepare() it again. Think of it as loading the file, getting ready for playback.
Edit
You have already attached your bassboost with your mediaplayer when it created. So remove this mp.attachAuxEffect(boost.getId());
Related
I have searched and researched stackoverflow and google but can't find any answer to MY question. I've found other question and answers but that were related to sounds saved in the app but I'm creating an app which gets data from Parse server, so it gets mp3 files and display these in listview and than when an item is clicked it plays that track. But here comes the problem: When you play a sound and click on another one, the first just doesn't stop and the second starts to play.
I have tried with the following code but it's just not working.
Here's my code:
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final MediaPlayer mediaPlayer = new MediaPlayer();
final MediaPlayer scndmediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
scndmediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
if (mediaPlayer.isPlaying()) {
Toast.makeText(getContext(), "First is playing", Toast.LENGTH_SHORT).show();
try {
mediaPlayer.stop();
scndmediaPlayer.setDataSource(audioFileURL);
scndmediaPlayer.prepare();
scndmediaPlayer.start();
//soundtoolbar.setTitle(name);
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
if (scndmediaPlayer.isPlaying()){
scndmediaPlayer.stop();
}
Toast.makeText(getContext(), "First is starting", Toast.LENGTH_SHORT).show();
mediaPlayer.setDataSource(audioFileURL);
mediaPlayer.prepare();
mediaPlayer.start();
soundtoolbar.setTitle(name);
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
playPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying() || scndmediaPlayer.isPlaying()) {
mediaPlayer.pause();
scndmediaPlayer.pause();
playPause.setBackground(getContext().getDrawable(R.drawable.ic_play_arrow_white_24dp));
} else {
mediaPlayer.start();
scndmediaPlayer.start();
playPause.setBackground(getContext().getDrawable(R.drawable.ic_pause_white_24dp));
}
}
});
}
});
I've created 2 mediaplayers with the code above and when user clicks the play button it first checks if any of the player is running.
I'm trying to achieve the following: When user clicks the play button it checks if the (1st) mediaPlayer is running or not. If it's running, it has just to stop it and launch (2nd) scndmediaPlayer or viceversa... if second is playing it stops that and launch first one. so it will create a loop: 1st is playing? User clicks another button stop first. Launch second. User clicks another button. First is playing? No. Second is playing? Yes. Stop the second and launch the first.
But can't find where is the problem in my code.
Please help me with this. I'm trying to resolve it from 2 days but I'm unable...
Thanks :)
EDIT: I tried using one MediaPlayer and do the following: Check if mediaplayer is playing! No it isn't playing. Start it. User clicks the button again and it stops the mediaplayer and start it with new audioFileUrl. BUT. MediaPlayer is forgetting that it's playing. Seems like it just starts the track and than forget and to check if it's true i set a Toast: when mediaplayer isn't playing the toast shows and it's showing every time I click a track in the list which means it forget that it has a track which is playing...
EDIT 2: I managed to do the following: It plays the track. User clicks another track. It stops the mediaplayer but doesn't play the new track. User click once again. It plays the new track. User clicks the new track and the app crashes...
EDIT 3: Posting my entire class:
public class MyAdapter extends ParseQueryAdapter<ParseObject> {
public Button playPause, next, previous;
public Toolbar soundtoolbar;
boolean isPlaying = false;
public MyAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("MyClass");
query.orderByDescending("createdAt");
return query;
}
});
}
#Override
public View getItemView(final ParseObject object, View v, final ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.activity_audio_files_item, null);
}
super.getItemView(object, v, parent);
final Button play = (Button) v.findViewById(R.id.play);
playPause = TabFragment1.playPause;
next = TabFragment1.next;
previous = TabFragment1.previous;
soundtoolbar = TabFragment1.soundtoolbar;
final ParseFile descr = object.getParseFile("audiofile");
final String name = object.getString("name");
final String audioFileURL = descr.getUrl();
final SlidingUpPanelLayout slidingUpPanelLayout = TabFragment1.spanel;
play.setText(name);
final MediaPlayer mediaPlayer = new MediaPlayer();
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isPlaying != true) {
Toast.makeText(getContext(), name+" is playing", Toast.LENGTH_SHORT).show();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(audioFileURL);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
soundtoolbar.setTitle(name);
slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
mediaPlayer.start();
isPlaying = true;
}
});
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (SecurityException e2) {
e2.printStackTrace();
} catch (IllegalStateException e3) {
e3.printStackTrace();
} catch (IOException e4) {
e4.printStackTrace();
} catch (NullPointerException e5) {
e5.printStackTrace();
}
} else {
mediaPlayer.stop();
Toast.makeText(getContext(), "Starting "+name, Toast.LENGTH_SHORT).show();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
mediaPlayer.setDataSource(audioFileURL);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
soundtoolbar.setTitle(name);
slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (SecurityException e2) {
e2.printStackTrace();
} catch (IllegalStateException e3) {
e3.printStackTrace();
} catch (IOException e4) {
e4.printStackTrace();
} catch (NullPointerException e5){
e5.printStackTrace();
}
}
playPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
playPause.setBackground(getContext().getDrawable(R.drawable.ic_play_arrow_white_24dp));
} else {
mediaPlayer.start();
playPause.setBackground(getContext().getDrawable(R.drawable.ic_pause_white_24dp));
}
}
});
}
});
return v;
}
}
Somebody please help...
i suggest you to checkout SoundPool . İt ll helps you. And one more , may be you ll put media urls to array or something like this.And use one mediaPlayer By the way, you ll avoid from two mediaPlayer and avoid from memory leak.
http://developer.android.com/reference/android/media/SoundPool.html
I am using MediaPlayer to playback audio file of .WAV format. When I debug the app I'm able to play file but when I run the app file is not playing.
There is a similar issue regarding this but no solution is provided.
Below is the code which i am using
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(mContext, fileUri);// uri of the audio file
mMediaPlayer.prepare();
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.start();// to start the playback of the audio file
This code is working only in debug mode but not in normal mode.
Thanks
This is what I am saying:
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp.reset();
mp.setDataSource(url.toString());
mp.setOnErrorListener(this);
mp.setOnPreparedListener(this);
mp.setOnCompletionListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
Log.e(TAG, "SecurityException");
} catch (IllegalStateException e) {
Log.e(TAG, "IllegalStateException");
} catch (IOException e) {
Log.e(TAG, "IOException");
}
mp.prepareAsync();
}
});
Now, override method onPrepared().
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
Comment below if you face any issue in this.
I tried to use the lib prebuilt, but couldn't, I got error on - FFmpegMediaPlayer mp = new FFmpegMediaPlayer();
OR can somebody to recommend good Android MediaPlayer(minSdkVersion >=9) library for playing stream audio?
GIT URL for FFmpegMediaPlayer
have sample code :
FFmpegMediaPlayer mp = new FFmpegMediaPlayer();
mp.setOnPreparedListener(new FFmpegMediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(FFmpegMediaPlayer mp) {
mp.start();
}
});
mp.setOnErrorListener(new FFmpegMediaPlayer.OnErrorListener() {
#Override
public boolean onError(FFmpegMediaPlayer mp, int what, int extra) {
mp.release();
return false;
}
});
try {
mp.setDataSource("<some path or URL>");
mp.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Want to do a audio streaming for a URL. The same URL is playing a live radio while running on the mobile browser. But while using MediaPlayer for android app there is no output. It gives the following error.
09-07 05:16:37.539: E/MediaPlayer(1265): error (1, -2147483648)
09-07 05:16:37.539: W/System.err(1265): java.io.IOException: Prepare failed.: status=0x1
My code sample is :
try {
Log.i("Audio Streaming", "start-->");
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(URL_OF_AUDIO);
mediaPlayer.prepare();
mediaPlayer.start();
} 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();
}
make sure that URL which is passing is valid and check the androidmanifest whether it is having the access permission.
use onprepared listener to start the video to avoid some unwanted exceptions.
try this one it works fine........
public class test extends Activity implements OnErrorListener, OnPreparedListener {
private MediaPlayer player;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.setDataSource("http://www.hubharp.com/web_sound/BachGavotte.mp3");
player.setOnErrorListener(this);
player.setOnPreparedListener(this);
player.prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onDestroy() {
super.onDestroy();
player.release();
player = null;
}
#Override
public void onPrepared(MediaPlayer play) {
play.start();
}
#Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
return false;
}
}
I have an activity which has a series of buttons which when pressed should play an audio file. I have been trying to implement this using MediaPlayer however I cant get it to work.
Here is the code I have been trying:
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setDataSource(this, R.raw.greet_1);
mp.prepare();
mp.start();
}
});
The setDateSource method doesnt seem to work, can anyone tell me where I am going wrong?
I would like to then set the mediaPlayer to the relevant audio file based on which button is pressed, is this possible?
Updated
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
Uri myUri = Uri.parse(R.raw.greet_1);
mp.setDataSource(GreetingsLesson.this, R.raw.greet_1);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
try this:
final MediaPlayer mp = new MediaPlayer();
Button ger1play = (Button) findViewById(R.id.ger1play);ger1play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mp.setDataSource(CurrentActivity.this, R.raw.greet_1);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Ifyou want to send the media player object with one of the files fromapplication raw resources or from application assets files you can dothat as follows:
try {
AssetFileDescriptor fd = getResources().openRawResouceFd(R.raw.greet_1);
mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
mp.start();
fd.close();
} catch (IllegalArgumentException e) {
// handle exception
} catch (IllegalStateException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
Why not just use
mp = MediaPlayer.create(this, R.raw.greet_1);
Then you don't need the prepare or start.
Are you running this in an emulator? If so check your AVD manager has under hardware, the property "Audio playback support | yes" added