当前位置:网站首页>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(){
}
}
}
边栏推荐
- 在企业级开发过程中我发现有位同事用select * from where 条件 for update
- Text editor for QT project practice - Episode 12
- 2021-04-18
- 百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑
- Golang示例续期锁:Redis+Channel+sync.Mutex
- Databinding quick start (still using findviewbyid?)
- Mobile security tool apktool
- Redis + Lua implementation of distributed interface current limiting
- Qiniu cloud uploads video to get the first frame of video
- I brush the question I - copy the linked list with random pointer
猜你喜欢

Thermodynamic diagram display correlation matrix

If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
![[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS](/img/fc/6b36759570b9b1007d2640009576d9.png)
[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS

Apk decompiled method (not confused)
![[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem](/img/01/5ec4e5dae1748dce3d5dee33a24b0b.png)
[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem

108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)

大厂高频软件测试面试题和答案都帮你准备好啦,备战金九银十

Syntax highlighting of rich text

傳輸層 以字節為單比特的滑動窗口技術

Add information on the left and add parts on the right of the status bar
随机推荐
adb shell getevent
Use of JMeter
Use and click of multitypeadapter in recycleview
2022熔化焊接与热切割复训题库模拟考试平台操作
2022 melting welding and thermal cutting recurrent training question bank simulated examination platform operation
I 刷题 I — 复制带随机指针的链表
adb shell getevent
Paint rounded rectangle
Punch smart spirit 1. The brand is attractive. What is the strength of the product?
Encryption and encoding resolution
Kubernetes 架构核心组件工作原理解析
Text border format and text block of rich text
Previous basic review (link)
Hot fix sophix multi-channel patch solution
The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products
打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?
Use of JMeter easynmon
QT (35) - operate excel qxlsx qaxobject
傳輸層 以字節為單比特的滑動窗口技術
Virtual machine - network configuration