当前位置:网站首页>Using bindservice method to pause music playing
Using bindservice method to pause music playing
2022-06-25 01:05:00 【I am at odds with 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=" Binding services " android:onClick="bangdingfuwu" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" stop it " android:onClick="tingzhi"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Play " 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, " Binding service succeeded ", 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(){
}
}
}
边栏推荐
- adb shell sendevent
- I 刷题 I — 复制带随机指针的链表
- 扎克伯格上手演示四款VR头显原型机,Meta透露元宇宙「家底」
- Thingsboard - rest API obtains and refreshes tokens
- Practical operation notes - notebook plus memory and ash cleaning
- 重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)
- Only positive integers can be entered in the text box
- Technologie des fenêtres coulissantes en octets dans la couche de transmission
- Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
- 智能合约安全审计入门篇 —— delegatecall (2)
猜你喜欢

使用 Loki 微服务模式部署生产集群

Practical operation notes - notebook plus memory and ash cleaning

JS Chapter 1 Summary

Punch smart spirit 1. The brand is attractive. What is the strength of the product?

2022年起重机司机(限桥式起重机)考试题库模拟考试平台操作

I 刷题 I — 复制带随机指针的链表

Preliminary understanding of qtoolbutton

Databinding quick start (still using findviewbyid?)

Syntax highlighting of rich text

Custom animation (simulated win10 loading animation) - Optimization
随机推荐
Input series
Mobile security tool apktool
Scala object blending trait
改造一下 BeanUtils,优雅的实现 List 数据拷贝
Practical operation notes - notebook plus memory and ash cleaning
Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise
C#和C 的CAN通信实验
Scala trait exercise
LLVM TargetPassConfig
Rich text tables, lists, pictures
Scala IO read by line
Using tcp/udp tools to debug the yeelight ribbon
QT(36)-rapidjson解析嵌套的json
Can communication experiment between C and C
移动安全工具-apktool
Tiktok wallpaper applet source code
Introduction to smart contract security audit delegatecall (2)
【微服务|Sentinel】簇点链路|微服务集群环境搭建
实现mnist手写数字识别
Default methods for Scala sample classes