Music playback controls

Source: Internet
Author: User

First, Musicactivity.java

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_musicservice);
Button start= (button) Findviewbyid (R.id.button_start);
Button stop= (button) Findviewbyid (r.id.button_stop);
Start.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View arg0) {
StartService (New Intent ("Com.yarin.Android.MUSIC"));
}
});
Stop.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View arg0) {
StopService (New Intent ("Com.yarin.Android.MUSIC"));
}
});
}

Second, Musicservice.java

private static final String tag= "Musicservice";
Create a MediaPlayer Object
Private MediaPlayer player;
@Override
Public IBinder Onbind (Intent arg0) {
return null;
}
@Override
public void OnCreate () {
Super.oncreate ();
try {
Loading music files
Player = Mediaplayer.create (this, r.raw.speedwagon);//raw Directory has Speedwagon.mp3 file
Start playback
Player.start ();
} catch (Exception e) {
LOG.I (TAG, e.tostring ());
}
}
@Override
public void OnDestroy () {
Super.ondestroy ();
try {
Player.stop ();
} catch (Exception e) {
LOG.I (TAG, e.tostring ());
}
}

Third, Androidmanifest.xml

<!--music playback Control--
<service android:name= ". Musicservice ">
<intent-filter>
<action android:name= "Com.yarin.Android.MUSIC" >
</action>

<category android:name= "Android.intent.category.default" >
</category>
</intent-filter>
</service>

<activity
Android:name= ". Musicactivity "
Android:label= "examples_0303" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

Music playback controls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.