Sounds/.mp3 in Android.
MediaPlayer:
The Android multimedia framework includes support for playing variety of common media types, so that you can easily integrate audio, video and images into your applications. You can play audio or video from media files stored in your application's resources (raw resources), from standalone files in the filesystem, or from a data stream arriving over a network connection, all using MediaPlayer APIs.This document shows you how to write a media-playing application that interacts with the user and the system in order to obtain good performance and a pleasant user experience.
Steps:
First create a project with button or toggle or switch button.
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sw"
android:text="Music Off "
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:src="@drawable/on"
android:layout_centerHorizontal="true"
android:id="@+id/on" android:layout_marginTop="30dp"
/>
Now create a directory in res directory and named it as raw
Now copy paste sound media files in raw directory.
Now go to java class and change in oncreate:
ImageButton off;
Switch aSwitch;
off = (ImageButton) view.findViewById(R.id.off);aSwitch = (Switch) view.findViewById(R.id.sw); final MediaPlayer mp = MediaPlayer.create(view.getContext(),R.raw.wholetdogout); aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ aSwitch.setText("Music On "); if(mp.isPlaying()){ mp.seekTo(0); }else{ mp.start(); } }else{ aSwitch.setText("Music Off "); mp.pause(); } } });final MediaPlayer bb = MediaPlayer.create(view.getContext(),R.raw.shutteraa);off.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (hasflash==1){ parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); camera.stopPreview(); }else { Toast.makeText(getActivity(),"Please Install Camera",Toast.LENGTH_SHORT).show(); } on.setVisibility(View.VISIBLE); imgvu.setImageResource(R.drawable.rszoff); off.setVisibility(View.GONE); if(bb.isPlaying()) { bb.seekTo(0); }else{ bb.start(); } } });
No comments:
Post a Comment