How to associate .mp3 files in Android Studio - java

I want to play an mp3 file based on a list view item click however based on my code went I run my app this window appears and so due to lack of an audio option I really don't know which one of these I need to select in order to associate my .mp3 files.
mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//When clicked, go to specific activity
if (position == 0) {
if(mp.isPlaying())
{
mp.stop();
mp.reset();
}
try {
AssetFileDescriptor afd = getAssets().openFd("chimes.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});

You don't have to associate anything this is only used to OPEN the file on the host machine, has nothing to do with the device.

you can drag and drop mp3 file in res folder android studio automatically dectect it

Related

How to check if an audio is playing with Android MediaPlayer?

In a situation where I have a listview that in each list item when clicking is an audio and is played when clicked on the desired item.
When you click on multiple items, the audio blends.
I would like to know how I can click on an item and play and if I want another item, when clicking it the previous audio stops and the audio of the clicaco item starts.
I tried to use the code below, but audios do not stop when I click on another item.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mediaPlayer = MediaPlayer.create(MainActivity.this, caminhoAudio[position]);
if ( !(mediaPlayer.isPlaying()) )
{
tocarSom();
}
}
});
}
public void tocarSom() {
if (mediaPlayer != null)
{
mediaPlayer.start();
}
// LIBERAR MEMÓRIA
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
};
});
}
Thank You!!!
Well, if your methode startAudio() automatically do everything needed to start to play a song, you have to call startAudio() after the mediaPlayer.release();
Do something like this.
if(mMediaPlayer!=null)
{
mMediaPlayer.release();
mMediaPlayer=null;
}
if(mMediaPlayer != null) {
//Fist stop the current playing raw file
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
//Then Play the selected raw file.
tocarSom();
} else {
//If nothing is playing then Play the selected raw file or audio
tocarSom();
}

Each item of a listView playing a different (short) media file. After so many clips I hear no more sound

I am making a sound board app. I have a String List of sound descriptions that populate the listView. My onItemClick takes in a switch statement that assigns each sound to it's string description from the list. The sounds work when the list item is clicked, but after so many clicks, all the sounds stop. the switch statement looks like this in my ItemOnClick:
switch (soundName){
case "magic whoosh":
mediaPlayer = MediaPlayer.create(context, R.raw.magic_whoosh);
mediaPlayer.start();
break;
case "magic poof":
mediaPlayer = MediaPlayer.create(context, R.raw.magic_poof);
mediaPlayer.start();
break;
}
As requested a possible answer;
//declare instance variable
static MediaPlayer mediaPlayer;
//handle listview on item click
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mediaPlayer==null){
mediaPlayer=new MediaPlayer();
try{
mediaPlayer.setDataSource(//datasource based on position);
mediaPlayer.prepare(); //this uses the same UI thread
mediaPlayer.start();
}catch(Throwable e){
//handle possible errors
}
}else{
if(mediaPlayer.isPlaying())mediaPlayer.stop();
mediaPlayer.reset();
try{
mediaPlayer.setDataSource(//datasource based on position);
mediaPlayer.prepare(); //this uses the same UI thread
mediaPlayer.start();
}catch(Throwable e){
//handle possible errors
}
}
}
});

Android multi-fragment layout in landscape mode

Although I got my code to work, I now have no idea what it's actually doing.
My app is an RSS reader, and the main content is in a Fragment containing a ListView with NewsStory objects. When a list item is clicked, it opens an Intent with the website linked from the RSS.
Now the problem is, I don't understand the Intent here, it's not the way I've ever used Intents before.
Also, I have to make it so that when I change the orientation, the original profile Fragment takes up the left half of the screen and the linked webpage takes up the right half of the screen. I've tinkered around with it, to no avail. I did a bit of research on orientation changes, but I feel like doing things with Fragments always changes how everything works. Anyway, here's the Fragment code. Any thoughts would be greatly appreciated.
public class HeadlineFragment extends Fragment {
EditText input;
Button search;
ListView headlines;
NewsDataSource ds;
public HeadlineFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_headline,container,false);
input = (EditText)v.findViewById(R.id.txtInput);
search = (Button)v.findViewById(R.id.btnSearch);
headlines = (ListView)v.findViewById(R.id.listView);
try {
ds = new NewsDataSource();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
headlines.setAdapter(new NewsDataSourceAdapter(this.getActivity(), ds));
headlines.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
String url = NewsDataSource.stories[position].getLink();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
return v;
}
/*
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
*/
}
Firstly, regarding that Intent, its an implicit Intent. When you set the action as ACTION_VIEW and add a URI / URL as an extra, the OS gets the message that you want to open an app that can navigate to that URI / URL.
Secondly, for showing a two-pane layout in landscape mode, you'll have to show that RSS content in a Fragment instead of an Activity as you are currently doing, and you'll have to display those Fragments side-by-side in an Activity in landscape mode. See the Retrieving a List of Contacts example for a really good explanation of how to display a multi-pane master detail layout in portrait mode.
References:
1. Intents and Intent Filters.
2. Planning for Multiple Touchscreen Sizes.

Set wallpaper using RecyclerView

I need to set an image as wallpaper using RecyclerView. In my adapter i'm using this code:
#Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
final GridItem nature = mItems.get(i);
viewHolder.tvspecies.setText(nature.getName());
viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
viewHolder.imgThumbnail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("CLick",nature.toString());
try {
wallpaper.setResource(mItems.get(i));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
so onClick i should set the image as wallpaper but under "setResource" word i have this error:
The method setResource(int) in the type WallpaperManager is not applicable for the arguments (GridItem)
How can i set the item selected from the adapter as wallpaper?
The reason your program is not working, the method setResource(int) takes in int type as a parameter and not GridItemtype.
Reading more from the documentation
Change the current system wallpaper to the bitmap in the given
resource. The resource is opened as a raw data stream and copied into
the wallpaper; it must be a valid PNG or JPEG image.
also make sure your app have permission to set wallpaper in manifest
This method requires the caller to hold the permission SET_WALLPAPER.

audio player app for android

hi i have created a new mp3 player app for my owm. In that i am playing songs from a folder where its been stored, the problem is when one song is in play and when i click the next song both gets played, i want the other one to be stopped. the following is my code
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Log.e("", "aaaaa "+arg2);
if(arg2==0)
{
mplayer = MediaPlayer.create(aply.this, R.raw.a);
mplayer.start();
}
else
{
mplayer = MediaPlayer.create(aply.this, R.raw.download);
mplayer.start();
}
mplayer.start();
}
});
pls help me in this....
you can check the playing status, if it is playing you have to stop the player and reset the contents and issue the command Play.
if (mplayer.isPlaying()) {
mp.stop();
}
mplayer.reset();
mplayer.setDataSource(abs_filepath);//set the path
mplayer.prepare();
mplayer.start();
above a sample code
Don't you want a check like this:
if (mplayer != null && mplayer.isPlaying()) {
mplayer.stop();
}
at some point before you start your next song?

Categories

Resources