当前位置:网站首页>bindservice方法实现音乐播放暂停
bindservice方法实现音乐播放暂停
2022-06-24 20:18:00 【我与c语言不共戴天】
xml
<?xml version="1.0" encoding="utf-8" ?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定服务" android:onClick="bangdingfuwu" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" android:onClick="tingzhi"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放" android:onClick="bofan"/> </LinearLayout>
java
package com.commerce.mybindservicemusic;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private MyServiceConnection myConn;
private MyService.MyBind myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void bangdingfuwu(View view) {
if (myConn==null)
{
myConn =new MyServiceConnection();
}
Intent service = new Intent(MainActivity.this,MyService.class);
bindService(service,myConn,BIND_AUTO_CREATE);
}
public void tingzhi(View view) {
myBinder.tingzhi();
}
public void bofan(View view) {
}
private class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
myBinder=(MyService.MyBind) iBinder;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
}
service
package com.commerce.mybindservicemusic;
import android.app.Service;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaParser;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
import java.io.IOException;
public class MyService extends Service {
private MediaPlayer mp;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
//
TODO: Return the communication channel to the service.
Toast.makeText(MyService.this, "绑定服务成功", Toast.LENGTH_LONG).show();
if (mp !=null){
if (!mp.isPlaying()) {
} else {
mp.stop();
}
mp.release();
}
mp=new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}
});
AssetFileDescriptor file =getResources().openRawResourceFd(R.raw.music);
try{
mp.setDataSource(file.getFileDescriptor(),file.getStartOffset(),file.getLength());
file.close();
mp.prepareAsync();
}catch (IOException e){
e.printStackTrace();
}
return new MyBind();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
public class MyBind extends Binder{
public void tingzhi(){
mp.pause();
}
public void bofan(){
}
}
}
边栏推荐
- Tiktok wallpaper applet v1.0.2 function, new arrival function
- The interview questions and answers for the high-frequency software test of Dachang help you prepare for the golden nine silver ten
- [microservices sentinel] cluster link | microservices cluster environment construction
- 我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗
- Kibana installation via kubernetes visual interface (rancher)
- Registration method of native method in JNI
- A plug-in framework for implementing registration free and login verification with hook technology
- Easy to wear - drop down radio
- Practical operation notes - notebook plus memory and ash cleaning
- redis + lua实现分布式接口限流实现方案
猜你喜欢

Apk decompiled method (not confused)

The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products

【Redis实现秒杀业务②】超卖问题的解决方案

Xcode预览(Preview)显示List视图内容的一个Bug及解决

2022年全国最新消防设施操作员(高级消防设施操作员)模拟题及答案

2022r1 quick opening pressure vessel operation test questions and answers

在企业级开发过程中我发现有位同事用select * from where 条件 for update

2022年危险化学品经营单位安全管理人员考试试题及模拟考试

2022R1快开门式压力容器操作考题及答案

Text editor for QT project practice - Episode 10
随机推荐
Text editor for QT project practice -- Episode 9
Garbage collection of C closure
我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗
Scala sample object
Can communication experiment between C and C
Redis + Lua implementation of distributed interface current limiting
108页(4万字)未来公寓智能化设计平台项目方案建议书2022版
The interview questions and answers for the high-frequency software test of Dachang help you prepare for the golden nine silver ten
ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
Default methods for Scala sample classes
Text editor for QT project practice - Episode 10
I brush the question I - copy the linked list with random pointer
Use and click of multitypeadapter in recycleview
Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar
Previous basic review (link)
Tiktok wallpaper applet source code
Mobile security tool jarsigner
[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem
Wallpaper applet wechat applet
Databinding quick start (still using findviewbyid?)