Code to create MP3 (Music) player in android studio - Android programming.

No comments

//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.dharmik.p5">     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />     <application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsRtl="true"         android:theme="@style/AppTheme">         <activity android:name=".MainActivity">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>         <service             android:name=".MyService"             android:enabled="true"             android:exported="true"/>     </application> </manifest>
//strings.xml
<resources>     <string name="app_name">P5</string>     <string name="start">Start</string>     <string name="stop">Stop</string> </resources>
//MyService.java
package com.example.dharmik.p5; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; public class MyService extends Service {     public MyService() {     }     MediaPlayer mp;
    @Override     public IBinder onBind(Intent intent) {         // TODO: Return the communication channel to the service.         throw new UnsupportedOperationException("Not yet implemented");     }
    @Override     public void onCreate() {         super.onCreate();         mp=MediaPlayer.create(getApplicationContext(),R.raw.bkh);         mp.start();     }     @Override     public void onDestroy() {         super.onDestroy();         mp.stop();     } }
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_music__player"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.example.dharmik.p5.MainActivity">     <Button         android:text="@string/start"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:onClick="start"         android:layout_marginStart="62dp"         android:layout_marginTop="152dp"         android:id="@+id/btn_start"         android:layout_alignParentTop="true"         android:layout_alignParentLeft="true"         android:layout_alignParentStart="true"         android:layout_marginLeft="62dp" />     <Button         android:text="@string/stop"         android:layout_width="wrap_content"         android:onClick="stop"         android:layout_height="wrap_content"         android:id="@+id/btn_stop"         android:layout_alignBaseline="@+id/btn_start"         android:layout_alignBottom="@+id/btn_start"         android:layout_alignParentRight="true"         android:layout_alignParentEnd="true"         android:layout_marginRight="38dp"         android:layout_marginEnd="38dp" /> </RelativeLayout>
//MainActivity.java
package com.example.dharmik.p5; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity {     Button start,stop;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }     public void start(View view){         Intent intent=new Intent(this,MyService.class);         startService(intent);     }     public void stop(View view){         Intent intent=new Intent(this,MyService.class);         stopService(intent);     } }
//Output

No comments :

Post a Comment